(我在下面所描述的本地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