Linux小技巧2--git与GitHub学习笔记

Linux小技巧2--git与GitHub学习笔记

近日学习了git与GitHub,内容是麦子学院老师关于git与GitHub的教程,话不多说先贴在此处以便于自己查看,也便于需要的人学习!后续将逐渐补充一些git常见错误处理相关的知识。

1、git 基础知识

# 删除 untracked files
git clean -f
 
# 连 untracked 的目录也一起删掉
git clean -fd
 
# 连 gitignore 的untrack 文件/目录也一起删掉 (慎用,一般这个是用来删掉编译出来的 .o之类的文件用的)
git clean -xfd
 
# 在用上述 git clean 前,墙裂建议加上 -n 参数来先看看会删掉哪些文件,防止重要文件被误删
git clean -nxfd
git clean -nf
git clean -nfd

git中没有被跟踪的文件一般为红色,跟踪单未被提交的维绿色

错误1
Git 提示fatal: remote origin already exists 错误解决办法
最后找到解决办法如下: 
1、先删除远程 Git 仓库
$ git remote rm origin
2、再添加远程 Git 仓库
$ git remote add origin git@github.com:FBing/Java-code-generator
如果执行 git remote rm origin 报错的话,我们可以手动修改gitconfig文件的内容
$ vi .git/config
把 [remote “origin”] 那一行删掉就好了。

错误2
xg@xg:~/Desktop/git-test$ git push -u origin master
Username for 'https://github.com': xg0719
Password for 'https://xg0719@github.com':
To https://github.com/xg0719/test.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/xg0719/test.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解决方法:
此时很多人会尝试下面的命令把当前分支代码上传到master分支上。
$ git push -u origin master
但依然没能解决问题
出现错误的主要原因是github中的README.md文件不在本地代码目录中
可以通过如下命令进行代码合并【注:pull=fetch+merge]
git pull --rebase origin master
执行上面代码后可以看到本地代码库中多了README.md文件
此时再执行语句 git push -u origin master即可完成代码上传到github

错误3 
对于使用git status -u (-u可以去掉)时候,出现
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
解决办法:
 git pull --rebase 即可

错误4
ECS服务器ssh登录提示“Permission denied, please try again
原因:密码错误,系统不允许远程登录
密码错误重新输入密码,远程登录被禁止就启动远程root登录就好了
1、在ssh提示上述信息时,可以在管理控制台通过Web VNC连接管理终端连接服务器进行测试,如果使用root和密码可以登录,说明帐号与密码是正确的。
2、检查/etc/ssh/sshd_config中的PermitRootLogin,该参数指定是否允许root用户通过ssh远程连接服务器。PermitRootLogin设置yes时表示允许root通过ssh连接服务器,为no时为不允许。



拿到项目后,先克隆代码到本地,然后在新建分支,代码写在新建分支中;若无误则将代码合并到主分支中,以提高代码的延续性

20、文件忽略
当对文件进行忽略后,更改文件内容后不会影响版本库(不提交不会影响其它命令的正常执行)

包括项目忽略和全局忽略两种

19、免密码登录下  --待看
18、免密码登录上  --待看


17、Git服务器的搭建
(1)在远程服务器上面新建一个git服务器
ssh root@IpAddr(域名) 进入服务器,
cd /opt
mkdir git 
初始化git仓库
git init --bare test.git
(2)本地新建git工作区
touch index.html
git add index.html
git commit -am "add index"
git remote -v  查看远程关联
git remote add origin ssh://root@ip(域名)/opt/git/test.git 
git push -u origin master
然后git clone ssh://root@域名/opt/git/test.git 将其克隆到本地


16、Git与GitHub-lias别名
git config --global alias.ci commit 将commit取别名为ci
修改别名后git ci -m "chagne commit to ci" 中的ci和commit就一样了
例如常见的别名为 checkout co;checkstatus ct;log  
git config --global alias.lg 'log --oneline' 
注意:学习视频中最后一个log的参数命名
 git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%C(bold blue)<%an>%Creset' --abbrev-commit"

15、 fork 和pull request
打开一个github.com账号如:
https://github.com/xg
随意找一个项目,如t1,点击右上角的Fork就看以将该项目分支到自己的github中,Fork的项目中包含原作者的信息,当我们对Fork的版本修改好了之后,我们可以创建一个pull request来通知原作者;
原作者收到pull request后就可以看到pull的信息,如果觉得合适的话可以将其合并到自己的版本库中,类似于commit merge功能;
当发现合并错误的时候,我们可以用Revert撤销之前的合并


14、git pull和git fetch的区别--非常重要,需要继续看一遍
git fetch:相当于是从远程获取最新版本到本地,不会自动merge
git pull:相当于是从远程获取最新版本并merge到本地

对于1个项目,AB两个人都需要使用,如何让B修改的内容可以让A查看到?
B修改index.html后将其提交到GitHub 
git commit -am "add index.html"
然后将其提交到远程的dev中
git push origin dev

当B修改dev分支后,A需要获取修改信息,因此要讲dev分支pull到本地
git pull origin dev

内容1如下:
(1)
修改A的README.md并且提交
vi README.md 
添加如下内容:
this is a'dev
然后提交修改:
git commit -am "modify readme.md"
(2)
修改A的README.md并且提交
vi README.md 
添加如下内容:
this is b'dev
然后提交修改:
git commit -am "modify readme.md"
(3)
 将B中的更新提交到远程仓库中
 git push origin dev
然后对A进行git pull origin dev会出错
【
<<<<<<< HEAD
this is a'dev
=======
this is b1'dev
this is b'dev
>>>>>>> dcd521cd754062ee7065ef6902962bc4cd96fc3b
xg@xg:~/Desktop/testA$ git pull origin dev
U       README.md
Pull is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm <file>'
as appropriate to mark resolution, or use 'git commit -a'.
】
然后对A的README.md进行修改提交即可
git commit -am "modify readme.md"
提交后对A pull就正常了

内容2如下:
(1)
修改b的readme并且提交
然后git push origin dev中,使版本库的Readme内容发生改变
(2)
在A中git fetch origin dev
如果正常则合并分支
git merge origin/dev --no-ff

注意: pull 更新并和合并本地分支;但是fetch 更新远程分支,但是不合并本地分支,若远程分支达到要求就可以进行选择合并了



1)
git push -f
2)先把git的东西fetch到你本地然后merge后再push
$ git fetch
$ git merge
这2句命令等价于



13、git remote和git clone
git clone https://github.com/xg0719/test.git 将远程的代码克隆岛本地
git branch -r 查看本地和远程的对应关系

git checkout dev
git push -u origin dev 

git branch 查看本地分支
git branch -a 查看远程的分支
正常情况下git clone只克隆了master分支,如果想克隆dev分支,则应该如下操作:
git checkout -b dev origin/dev 将dev和master建立关联

git remote remove origin 将远程版本库和本地关联取消掉

xg@xg:~/Desktop/git-test$ git remote -v  查看本地和远程版本库的关联
origin  https://github.com/xg0719/test.git (fetch)
origin  https://github.com/xg0719/test.git (push)

git remote add origin https://github.com/xg0719/test.git 再次建立远程关联

git remote rename

12、github的简介
git是版本控制工具,github是托管平台
https://github.com/
github 提供public和private的仓库,一般公司不会发钱买private仓库,多自己搭建github服务器

关联本地git仓库到远程托管中心
git remote add origin https://github.com/xg0719/test.git
git push -u origin master
然后输入github的用户名和密码即可


11、GitHub -Bug分支和feature分支
git merge dev --no-ff -m "merge dev test"  合并分支

git checkout -b bug_001 创建并切换到分支bug_001
在dev中修改的index.html文件,在其它文件中的index.html也会有修改

git checkout dev
git stash   将dev中的文件进行封存
然后git checkout bug_001就会发现修改的index.html没有影响master和bug_001

git stash pop 还原封存的分支
git stash list 查看封存的列表
git stash  apply stash@{0}  执行某一个封存,其中stash@{0}为版本号

当项目中需要增加一个新功能的时候,一般先建立一个新的feature分支,该分支开发的好的话就合并到dev分支后者master分支,该分支不太好的话就直接删除即可 
git checkout -b feature_001

10、git merge下
(3)禁用快进的合并
git merge dev --no-ff -m "merge dev --no-ff" 即为增加了--no--ff(fast forward)

分支删除
git branch -d 分支名称
例如: git branch -d dev 删除dev分支
分支修改
git branch -m A B 将A分支修改为B
例如: git branch -m dev2 dev

9、git merge上
git merge dev 将dev分支的内容提交到当前分支

git diff 可以检测到文件的变化,检测到添加删除的某一行
git diff dev..master 比较两个分支的差别
(1)理想的合并
当A中文件比B中文件少的时候,将B合并到A中可以正常合并
(2)解决冲突的合并
当AB中都有某个文件的时候,若果只用git merge B 将B中文件合并到A中的时候会出现如下冲突:
Auto-merging readme
CONFLICT (add/add): Merge conflict in readme
Automatic merge failed; fix conflicts and then commit the result.
很明显有问题,此时查看有冲突的文件,内容如下:
<<<<<<< HEAD
readme master
=======
readme dev
>>>>>>> dev
其中=上面的维master中的内容,=下面的为dev中的内容,我们需修改有冲突的文件,使之以某个文件为准,如以dev分支文件为准则保留有=和>>>>dev之间的文件即可
然后再git add 和git comm即可完成合并

8、git branch
git branch 查看版本
分支可以存储文件,而不影响master分支
git branch branchName 创建分支
git checkout dev 切换到dev分支
使用分支之后,若改变从分支内容,主分支不会受到影响

进入某一个分支后,若修改某一个文件,则修改后需要 git add 和git commit来提交修改的文件到当前版本中;修改当前版本张的文件不会影响其它版本中文件内容

7、git reset 
git commit -m "add index.html"
git reset HEAD^ --soft   将某一个文件从提交的分支撤销到暂存区中,使用status时候提示有一个文件未提交(既在暂存区)
git reset HEAD^ --mixed  重置某一步并且并且将文件从分支和暂存区中撤回来,导致文件没有被跟踪(untracked files)
git reset HEAD^ --hard  重置版本库中的文件,并且本地文件也一起被重置了(人本地文件一起被删除),既一律返回到某个版本(直接影响工作区的文件,一般情况下不可以使用)
重制某一个提交,而不是改变所有的提交;get revert HEAD~n则在0-n中的提交将全部被撤销了
3241dd3
也可以使用版本号来重置版本git reset 版本号;
对于untracked的文件可以先使用git add filename 然后使用git commit提交到版本库中

6、git revert ;git reset 
git revert HEAD 撤销当前版本
git revert HEAD^ 撤销上一个版本
git revert HEAD^^  撤销上两个版本
git revert HEAD~8 返回第8个版本
对于revert处理删除版本库中的文件,还删除了本地文件;对于一个文件,若希望删除版本库中的文件,而不删除本地文件,则可以使用git rm 或git rm --cached两种方法

5、git rm
若把test.js加入到暂时存区后,再rm test.js,则需要删除暂存区test.js 或者回滚删除操作
git checkout -- test.js
或者git rm -f test.js
git checkout -- main.js 
git commit -m "rm main.js"

4、gitHub --git mv--待查看
git log 
git mv  对暂存区的文件(git add file)进行修改
git log --oneline  单行显示log信息

3、工作区和暂存区
git init  初始化工作区
git status  查看版本库内的信息
git add index.html  蒋index.html 添加到版本库中的暂存区中
git commit  将暂存区中的目录数量提交到master中,index.html中的内容都会存在objects中

2、git与github帮助文档
git --version
git help -a list all subcommands
gid help commandName 查看命令的功能
git help config 查看配置
git config --global user.name 'git-xg'
git config --global user.email '861653644@qq.com'
git --unset --global user.name 重置用户名称
git config list 查看用户配置
cd ~  ls -la (。gitconfig)

1、
GitKeeper
SVN 和 Git
SVN有一个代码控制中心,其它人只能从中根据权限获取相应代码,因此公司多用svn,而个人可以使用不受权限控制的git

2、git 经典用法

2.1 通过blame查看文件提交和修改信息
格式: git blame fileName

% git blame system.go
eb79077f (Andrew Kroh      2018-04-05 02:31:42 -0400  1) // Licensed to Elasticsearch B.V. under one or more contributor
eb79077f (Andrew Kroh      2018-04-05 02:31:42 -0400  2) // license agreements. See the NOTICE file distributed with
eb79077f (Andrew Kroh      2018-04-05 02:31:42 -0400  3) // this work for additional information regarding copyright
eb79077f (Andrew Kroh      2018-04-05 02:31:42 -0400  4) // ownership. Elasticsearch B.V. licenses this file to you under
......

3、git 常见错误处理

1)win下git 乱码解决方法
git log乱码解决方法:设置菜单
options->Text->Locale (zh_CN)->Character set(UTF-8)
确认后即可解决git log乱码问题。

git status乱码解决方法:命令设置,在git终端输入如下内容即可正常显示中文
git config --global core.quotepath false

2)git push -u出现不在分支上错误
git push -u
fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD) state now, use

git push origin HEAD:<name-of-remote-branch>
即将<name-of-remote-branch>改为对应的Branch Name即可

3)删除错误的commit 
git 新增加文件其状态为untracked,可用 git clean -nfd 查看会删除哪些文件和文件夹, 然后用git clean -fd删除不要的文件和文件夹;
通过git add .即可改变状态为new file,
通过git commit -m "infomation" 即可create mode,
通过git push -u推送到服务器

若在commit阶段发现提交错了,可通过如下方法撤销commit,
方法1:git reset --soft HEAD^(或者对应的上一个commitid),该操作后取消了错误的提交,但是取消的commit中add的文件仍然处于new file状态,若需要删除或者修改,操作后重新commit即可。
方法2:git reset --hard HEAD^ ,该操作取消了错误的提交,并返回到HEAD^对应提交状态,即删除了HEAD^提交后add的所有文件;
即:若取消commit时候需要保留部分较新的文件,使用soft,若需要删除所有的文件可以直接使用hard

4)git 删除服务器上错误的commit
git reset --hard HEAD^
git push --force 
先通过hard reset,然后force push即可删除远程错误的commit,该方法适合删除最新的错误提交;

如下方法可删除最后两次提交:
git reset --hard HEAD~2
git push --force 

使用该方法删除commit需要注意:在你commit之后别人又提交了新的commit,那在你强制推送之后,那别人的commit4也跟着一起消失了。

5) git pull出现 error: You have not concluded your merge错误
git pull origin pythontraining
error: You have not concluded your merge (MERGE_HEAD exists).
hint: Please, commit your changes before merging.
fatal: Exiting because of unfinished merge.
git status后发现:
$ git status
On branch pythontraining
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)
Changes to be committed:
        new file:   "python/Doc/python\344\275\234\344\270\232.xmind"
        new file:   python/pic/P01.PNG
解决方法:
git reset --merge,然后可以重新git pull了

6)git checkout出现commit your changes or stash错误
git checkout pythoncode
error: Your local changes to the following files would be overwritten by checkout:
        webbackend/xxx.md
Please commit your changes or stash them before you switch branches.
解决方法:git checkout webbackend/xxx.md ,然后直接再git checkout pythoncode

7) windows下git提交出现LF will be replaced by CRLF
$ git add .
warning: LF will be replaced by CRLF in scripts/shell/cbc3/nfs_mount.sh.
The file will have its original line endings in your working directory
$ git config --global core.autocrlf false
再次提交的时候就正常了,不会出现LF被替换的提示了。

8)sign_and_send_pubkey: signing failed: agent refused operation : Permission denied (publickey).
解决方法:chmod 0600 ~/.ssh/id_rsa

9) git push 出现has no upstream branch 错误
$ git checkout -b dev_A
$ git push
fatal: The current branch dev_A has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin dev_A
执行上述 set-upstream后,再次git push即可

10)不同项目|路径配置不同的git config信息

$ vim ~/.gitconfig 新增如下内容:
[includeIf "gitdir:~/files/code/github/"]
path = .gitconfig-user

$ cat ~/.gitconfig-user 新增如下内容:
[user]
	email = your-email@163.com
	name = your-name

测试:
$ git config --show-origin --get user.name
file:/home/xg/.gitconfig-user	your-name
$ git config --show-origin --get user.email
file:/home/xg/.gitconfig-user    your-email@163.com

参考 : 不同的项目使用不同的git配置 | 白发川的BLOG

11) linux 下git clone http每次都要输入密码

解决方法:$ git config --global credential.helper store

4、说明

以上是笔者关于git的学习笔记,内容仅供参考!

对应的服务器为GitHub和Gerrit。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昕光xg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值