2011-08-26 20 views
4

約翰和湯姆克隆相同的遠程回購。 遠程回購的內容是這樣的:Git將如何合併這個?

FileA.c 

約翰使本地提交這樣的,即它移動到FileA.c名爲John的文件夾並在本地提交。

John\FileA.c 

湯姆也使本地提交這樣的,即它移動到FileA.c一個名叫湯姆的文件夾並在本地提交。

Tom\FileA.c 

如果湯姆和約翰既推動當地回購到遠程回購,說GitHub的,怎麼會混帳合併呢?

我希望我明確自己。 謝謝!

回答

4

當你做git commit但不做git push,所做的更改將添加到您的本地存儲庫中,並且不會提交到全局回購。所以,無論誰先推送他的更改,他都不會發生任何衝突,因爲遠程服務器不知道其他用戶所做的更改尚未pushed。之後,當下一個用戶嘗試推送更改時,git將發出錯誤消息,表明全局repo已更改,您必須先取消更改。因爲這些更改都在同一個文件中,並且位置已經更改,所以會有衝突,這不會由git本身來解決。 下一個用戶將不得不再次進行更改並推送它以更新全局回購。

1

push期間,Git永遠不會合並。兩個開發者之一(窮人試圖推後)必須在本地解決衝突並告訴git移動文件的位置。沒有人工干預,Git無法知道文件應該放在哪裏。

# dev1 
git clone … 
mkdir John && git mv FileA.c John/FileA.c 
git commit -m 'move file to john subdir' 
git push origin master 

# dev2 
git clone … 
mkdir Tom && git mv FileA.c Tom/FileA.c 
git commit -m 'move file to tom subdir' 
git push origin master 
# git errors out: non-fast-forward, pull first to resolve potential conflicts 
git pull origin master 
# merge conflict in FileA.c 
# tell git which file to delete and which file to keep: 
git rm Tom/FileA.c && git add John.FileA.c 
git commit # creates a merge commit 
git push origin master 
2

的Git/SVN /等,做一個合併儘可能最佳:如果他們看到的代碼單獨的,非干擾線已添加/刪除/修改,那麼它會簡單地結合由約翰做了這些變化/FileA.c和Karen/FileA.c(它會成爲你的例子中的一個女孩= P的習慣)。如果合併腳本看到的是相同的代碼行已被編輯,那麼它會將其標記爲文件中的衝突。

我認爲這一點已經提出,但是這個過程發生在本地;無論是由凱倫還是約翰,取決於誰是最後的嘗試和推動者。最後推送的人將被通知從遠程回購中拉出並解決任何衝突,然後推送他們自己的更改。

1

第二次按下操作將失敗;合併必須在本地完成。在這種情況下會有衝突,必須手動解決。

0

從其他答案的關鍵點是,git不猜測應該發生什麼[使用一些預先編碼的推定],而是會警告用戶的困難,因爲它不會介意讀取各種用戶意圖。

聲稱能夠進行這種合併的大多數其他系統未通過千里眼測試 - 他們不知道你真正想要什麼。讓聰明的人(那就是你和我;-)做出決定。

1

git的好處在於,在不影響任何服務器的情況下輕鬆地嘗試這些東西。最後一個人推動的結果是:

02:32:54 ~/desktop/tom 
$ git push 
To /cygdrive/h/desktop/test 
! [rejected]  master -> master (non-fast-forward) 
error: failed to push some refs to '/cygdrive/h/desktop/test' 
To prevent you from losing history, non-fast-forward updates were rejected 
Merge the remote changes (e.g. 'git pull') before pushing again. See the 
'Note about fast-forwards' section of 'git push --help' for details. 

02:32:54 ~/desktop/tom 
$ git pull 
remote: Counting objects: 3, done. 
remote: Total 2 (delta 0), reused 0 (delta 0) 
Unpacking objects: 50% (1/2) Unpacking objects: 100% (2/2) Unpacking objects: 100% (2/2), done. 
From /cygdrive/h/desktop/test 
    307f68a..2539f44 master  -> origin/master 
error: refusing to lose untracked file at 'John/FileA.c' 
error: refusing to lose untracked file at 'John/FileA.c' 
CONFLICT (rename/rename): Rename "FileA.c"->"Tom/FileA.c" in branch "HEAD" rename "FileA.c"->"John/FileA.c" in "2539f448bb15d10f14bef74688dc3470975c2dbf" 
CONFLICT (rename/rename): Rename "FileA.c"->"Tom/FileA.c" in branch "HEAD" rename "FileA.c"->"John/FileA.c" in "2539f448bb15d10f14bef74688dc3470975c2dbf" 
Automatic merge failed; fix conflicts and then commit the result 

Git很不錯,並詢問哪個重命名是正確的。請注意,並非所有版本控制系統都能夠檢測到這種重命名/重命名衝突。