本文主要是介绍git 合并远程分支(带冲突),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
应用场景
团队中两人同时fetch了一个分支。 第一个人修改后提交,第二个人提交就失败。失败信息如下:
- error: failed to push some refs to 'git@git.oschina.net:jacarrichan/jacarrichan.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.
虽然使用“git push -f ”也可以提交,但是会将remote上第一个人的改动冲掉,太暴力了,不太好。下面说的是手动合并.
解决办法
1,获取远程分支更新,也就是第一个人提交的:
- git fetch origin
- git merge origin/master ##将origin/master合并到当前分支
- Auto-merging README.md
- CONFLICT (content): Merge conflict in README.md
- Automatic merge failed; fix conflicts and then commit the result.
- jacarri@JACARRI-PC /F/temp/mvn/jacarrichan (master|MERGING)
- $ git status
- # On branch master
- # You have unmerged paths.
- # (fix conflicts and run "git commit")
- #
- # Unmerged paths:
- # (use "git add <file>..." to mark resolution)
- #
- # both modified: README.md
- #
- no changes added to commit (use "git add" and/or "git commit -a")
现在我们再来看一下工作目录里的“README.md”文件的内容:
- jacarri@JACARRI-PC /F/temp/mvn/jacarrichanc (master|MERGING)
- $ cat README.md
- #jacarrichan
- Only for ochina git test.
- <<<<<<< HEAD
- 这是第二个人提交的......................
- =======
- 这是第一个人提交的
- >>>>>>> origin/master
- ![我的博客](http://service.t.sina.com.cn/widget/qmd/1597668397/99810a10/1.png ""
- )
“<<<<<<< HEAD“下面就是当前版本里的内容;而“=======”之下,“>>>>>>>origin/master”之上则表示测试分支里与之对应的有冲突的容。修复冲突时我们要做的,一般就是把“ <<<<<<< HEAD”,“=======”和“ >>>>>>> test”这些东东先去掉,然后把代码改成我们想要的内容。
需安装配置BeyondCompare,下载地址
- [diff]
- tool = bc3
- [difftool "bc3"]
- cmd = \"D:/application/BeyondCompare/BCompare.exe\" \"$LOCAL\" \"$REMOTE\"
- trustExitCode = true
- [difftool]
- prompt = false
- [merge]
- tool = bc4
- [mergetool "bc4"]
- cmd = \"D:/application/BeyondCompare/BCompare.exe\" \"$LOCAL\" \"$REMOTE\" \"$BASE\" \"$MERGED\"
- trustExitCode = true
- jacarri@JACARRI-PC /F/temp/mvn/jacarrichanc (master|MERGING)
- $ git mergetool README.md
- Merging:
- README.md
- Normal merge conflict for 'README.md':
- {local}: modified file
- {remote}: modified file
- Hit return to start merge resolution tool (bc4):
备注:图片上方的三个深蓝色圈住的和配置文件中的这个对应“ \"$LOCAL\" \"$REMOTE\" \"$BASE\" \"$MERGED\"”.
按照上述规则,我选择把双方的内容都添加进来,你采纳第一个或者另外写别的内容都可以。 保存后重新提交就顺利.
这篇关于git 合并远程分支(带冲突)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!