(我在下面所描述的本地git仓库的实验都发生了什么。无远程回购参与。)为什么git pull提供了合并的机会,但git push没有?
我创建2个地方分支机构lb1
和lb2
。它们从相同的提交节点开始。
git branch lb1
git branch lb2
我让它们相互上游。如下:
$ git checkout lb2
Switched to branch 'lb2'
$ git branch -u lb1
Branch lb2 set up to track local branch lb1.
$ git checkout lb1
Switched to branch 'lb1'
$ git branch -u lb2
Branch lb1 set up to track local branch lb2.
然后我通过对它们进行不同的改变使它们发散。如下图所示:
$ git branch -vv
* lb1 6774aa6 [lb2: ahead 1, behind 1] lb1 change
lb2 cdd1247 [lb1: ahead 1, behind 1] lb2 change
我目前lb1
。然后我试图一个分支(LB1)推到其他(LB2),看看会发生什么:
$ git push . lb1:lb2
To .
! [rejected] lb1 -> lb2 (non-fast-forward)
error: failed to push some refs to '.'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and 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.
所以,如果我的理解是正确的,它说,推分支的顶端(lb1
)的背后是其远程对方(lb2
)。我必须先结帐lb1
并从lb2
拉到整合lb2
的变化。
所以我这样做:(请注意,我在lb1
分支现在)
$ git pull
From .
* branch lb2 -> FETCH_HEAD
Auto-merging file1.txt
CONFLICT (content): Merge conflict in file1.txt
Automatic merge failed; fix conflicts and then commit the result.
所以git pull
使我有机会合并。
后,我合并,我可以推lb1
到lb2
:
$ git push . lb1:lb2
Total 0 (delta 0), reused 0 (delta 0)
To .
cdd1247..2dfb555 lb1 -> lb2
和版本图是这样的:
而且lb2
的变化是加入lb1
的日志历史:
总结:
- LB1 - 推 - > LB2:失败
- LB1 < - 拉 - LB2:有机会合并
我的问题是:
为什么git push
不提供合并的机会?从英语的角度来看,我认为push
和pull
是对称的。所以我期望他们有同样的结果。
一个失败的推动意味着分支不是快进,你会重写一些历史记录(或重新编入死亡历史记录) –
git pull是git fetch和git merge命令的组合。这就是它提供合并的原因。 git push是一个独立的命令。 – dunni
@AnthonySottile通过“分支”,我猜你是指推送的目标分支,在我的场景中为'lb2'。 – smwikipedia