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


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

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,下载地址 

配置 .gitconfig :C:\Users\${user.home}\.gitconfig

[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\"”.



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





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值