Git – 拉取(git pull)时的冲突

如果拉取时,遇到冲突怎么处理?

如前所诉,git pull相当于 git fetch 跟着一个 git merge。所以拉取遇到冲突的处理,和合并冲突处理是一样的。

拉取实际上可以看做是把远程分支集成到本地当前分支,类似分支之间的集成,除了合并还可以使用rebase。拉取时使用rebase,可使用命令:git pull --rebasegit pull -r。如果遇到冲突,与前面章节介绍过的rebase冲突一样处理。

正如在rebase一节中详细介绍的,保持一个干净的线性历史记录是有好处的,每次使用git pull时推荐使用git pull -r

总之,拉取时遇到冲突,与merge/rebase时的处理是一样的。

例子

引入冲突

引入冲突步骤:

  1. 修改远程仓库(github)上,master分支下的test1.txt文件中第一行文字,提交。
  2. 修改本地仓库上,master分支下的test1.txt文件中第一行文字,内容与远程仓库的修改不同,提交,不推送。
  3. 执行git fetch,以便可以使用git status查看冲突之处。
$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

nothing to commit, working tree clean

可以看到,本地master分支与远程origin/master分支分叉了。

拉取并解决冲突

这里我们尝试使用git pull -r,使用rebase来集成远程分支。

Kevin@qikegu MINGW64 /g/project/git-demo (master)
$ git pull -r
First, rewinding head to replay your work on top of it...
Applying: add 'abcde' to test1.txt
Applying: add 'fghij' to test1.txt
Applying: add test2.txt
Applying: change 1st line of test1.txt: 'file type is txt'
Using index info to reconstruct a base tree...
M       test1.txt
Falling back to patching base and 3-way merge...
Auto-merging test1.txt
CONFLICT (content): Merge conflict in test1.txt
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch' to see the failed patch
Patch failed at 0004 change 1st line of test1.txt: 'file type is txt'
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".

提示test1.txt有冲突,需要手动解决冲突。编辑test1.txt文件,确定要保留的内容,删除git添加的特殊内容,保存文件。

然后把文件添加到暂存区,然后继续rebase,执行命令git rebase --continue

Kevin@qikegu MINGW64 /g/project/git-demo (master|REBASE 4/7)
$ git add test1.txt

Kevin@qikegu MINGW64 /g/project/git-demo (master|REBASE 4/7)
$ git rebase --continue
Applying: change 1st line of test1.txt: 'file type is txt'
Applying: change 1st line of test1.txt: 'file name is test1.txt'
Using index info to reconstruct a base tree...
M       test1.txt
Falling back to patching base and 3-way merge...
Auto-merging test1.txt
Applying: add 'klmno' to test1.txt
Applying: modified:   test1.txt
Using index info to reconstruct a base tree...
M       test1.txt
Falling back to patching base and 3-way merge...
Auto-merging test1.txt



浙ICP备17015664号-1 浙公网安备 33011002012336号 联系我们 网站地图  
@2019 qikegu.com 版权所有,禁止转载