git 合并远程分支(带冲突)

来源:互联网 发布:设计 知乎 编辑:程序博客网 时间:2024/05/16 14:53

应用场景

团队中两人同时fetch了一个分支。 第一个人修改后提交,第二个人提交就失败。失败信息如下:

  1. error: failed to push some refs to 'git@git.oschina.net:jacarrichan/jacarrichan.git'  
  2. hint: Updates were rejected because the remote contains work that you do  
  3. hint: not have locally. This is usually caused by another repository pushing  
  4. hint: to the same ref. You may want to first integrate the remote changes  
  5. hint: (e.g., 'git pull ...') before pushing again.  
  6. hint: See the 'Note about fast-forwards' in 'git push --help' for details.  

虽然使用“git push -f  ”也可以提交,但是会将remote上第一个人的改动冲掉,太暴力了,不太好。下面说的是手动合并.  


解决办法

1,获取远程分支更新,也就是第一个人提交的:

  1. git fetch origin  


2,尝试由git带来的自动合并:

  1. git merge origin/master     ##将origin/master合并到当前分支  
如果两个分支的内容有差异,否则提示合并失败。

  1. Auto-merging README.md  
  2. CONFLICT (content): Merge conflict in README.md  
  3. Automatic merge failed; fix conflicts and then commit the result.  



看下当前的状态,寻找帮助信息:
  1. jacarri@JACARRI-PC /F/temp/mvn/jacarrichan (master|MERGING)  
  2. $ git status  
  3. # On branch master  
  4. # You have unmerged paths.  
  5. #   (fix conflicts and run "git commit")  
  6. #  
  7. # Unmerged paths:  
  8. #   (use "git add <file>..." to mark resolution)  
  9. #  
  10. #       both modified:      README.md  
  11. #  
  12. no changes added to commit (use "git add" and/or "git commit -a")  



现在我们再来看一下工作目录里的“README.md”文件的内容:

  1. jacarri@JACARRI-PC /F/temp/mvn/jacarrichanc (master|MERGING)  
  2. $ cat README.md  
  3. #jacarrichan  
  4.   
  5.   
  6.   
  7.   
  8. Only for ochina git test.  
  9.   
  10.   
  11. <<<<<<< HEAD  
  12. 这是第二个人提交的......................  
  13. =======  
  14. 这是第一个人提交的  
  15. >>>>>>> origin/master  
  16.   
  17.   
  18. ![我的博客](http://service.t.sina.com.cn/widget/qmd/1597668397/99810a10/1.png ""  
  19. )  

“<<<<<<< HEAD“下面就是当前版本里的内容;而“=======”之下,“>>>>>>>origin/master”之上则表示测试分支里与之对应的有冲突的容。修复冲突时我们要做的,一般就是把“ <<<<<<< HEAD”,“=======”和“ >>>>>>> test”这些东东先去掉,然后把代码改成我们想要的内容。


需安装配置BeyondCompare,下载地址 

  1. [diff]     
  2.     tool = bc3  
  3. [difftool "bc3"]     
  4.     cmd = \"D:/application/BeyondCompare/BCompare.exe\" \"$LOCAL\" \"$REMOTE\"    
  5.     trustExitCode = true      
  6. [difftool]     
  7.     prompt = false     
  8. [merge]  
  9.     tool = bc4     
  10. [mergetool "bc4"]     
  11.     cmd = \"D:/application/BeyondCompare/BCompare.exe\" \"$LOCAL\" \"$REMOTE\" \"$BASE\" \"$MERGED\"     
  12.     trustExitCode = true  


手动合并

  1. jacarri@JACARRI-PC /F/temp/mvn/jacarrichanc (master|MERGING)  
  2. $ git mergetool  README.md  
  3. Merging:  
  4. README.md  
  5.   
  6. Normal merge conflict for 'README.md':  
  7.   {local}: modified file  
  8.   {remote}: modified file  
  9. Hit return to start merge resolution tool (bc4):  

备注:图片上方的三个深蓝色圈住的和配置文件中的这个对应“ \"$LOCAL\" \"$REMOTE\" \"$BASE\" \"$MERGED\"”.



 按照上述规则,我选择把双方的内容都添加进来,你采纳第一个或者另外写别的内容都可以。  保存后重新提交就顺利.

0 0
原创粉丝点击