衝突:git
merge
在作合併的時候,是有必定的自動合併能力的:若是一個分支改了 A 文件,另外一個分支改了 B 文件,那麼合併後就是既改 A 也改 B,這個動做會自動完成;若是兩個分支都改了同一個文件,但一個改的是第 1 行,另外一個改的是第 2 行,那麼合併後就是第 1 行和第 2 行都改,也是自動完成。算法
但,若是兩個分支修改了同一部份內容,merge
的自動算法就搞不定了。這種狀況 Git 稱之爲:衝突(Conflict)。fetch
在現實的團隊開發中,是同時並行開發的,因此必然會出現當一人 push
代碼時,中央倉庫已經被其餘同事先一步 push
了的狀況。spa
示例:code
前置條件,有兩個本地倉庫,目錄分別是:test-marketing-testcase 和 test-marketing-testcase-another 最開始兩個目錄裏的內容是同樣的blog
commit
,而後 push
到 GitHub➜ test-marketing-testcase-another git:(master) touch 1.txt ➜ test-marketing-testcase-another git:(master) ✗ vi 1.txt ➜ test-marketing-testcase-another git:(master) ✗ git add . ➜ test-marketing-testcase-another git:(master) ✗ git commit -m "添加1.txt" [master bc77233] 添加1.txt 1 file changed, 3 insertions(+) create mode 100644 1.txt ➜ test-marketing-testcase-another git:(master) git push Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 277 bytes | 0 bytes/s, done. Total 3 (delta 1), reused 0 (delta 0) To git@git.souche-inc.com:testGroup/test-marketing-testcase.git d33cf17..bc77233 master -> master
2.切回 test-marketing-testcase 變回你本身,作一個不同的 commit
。開發
➜ test-marketing-testcase-another git:(master) cd /Users/chichi/Documents/gittest/test-marketing-testcase ➜ test-marketing-testcase git:(master) touch 2.txt ➜ test-marketing-testcase git:(master) ✗ vi 2.txt ➜ test-marketing-testcase git:(master) ✗ git add . ➜ test-marketing-testcase git:(master) ✗ git commit -m "添加2.txt" [master 7481b5a] 添加2.txt 1 file changed, 1 insertion(+) create mode 100644 2.txt
這個時候,遠端中央倉庫已經有了別人 push
的 commit
,如今你若是 push
的話:rem
➜ test-marketing-testcase git:(master) git push To git@git.souche-inc.com:testGroup/test-marketing-testcase.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'git@git.souche-inc.com:testGroup/test-marketing-testcase.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.
從上圖中的提示語能夠看出來,因爲 GitHub 的遠端倉庫上含有本地倉庫沒有的內容,因此此次 push
被拒絕了。這種衝突的解決方式其實很簡單:先用 pull
把遠端倉庫上的新內容取回到本地和本地合併,而後再把合併後的本地倉庫向遠端倉庫推送。it
git pull
此次的 git pull
操做並無像以前的那樣直接結束,而是進入了上圖這樣的一個輸入提交信息的界面。這是由於當 pull
操做發現不只遠端倉庫包含本地沒有的 commit
s,並且本地倉庫也包含遠端沒有的 commit
s 時,它就會把遠端和本地的獨有 commit
s 進行合併,自動生成一個新的 commit
,而上圖的這個界面,就是這個自動生成的 commit
的提交信息界面。另外,和手動的 commit
不一樣,這種 commit
會自動填入一個默認的提交信息,簡單說明了這條 commit
的來由。你能夠直接退出界面來使用這個自動填寫的提交信息,也能夠修改它來填入本身提交信息。io
在退出提交信息的界面後,此次 pull
就完成了:遠端倉庫被取到了本地,並和本地倉庫進行了合併。在這個時候,就能夠再 push
一次了。因爲如今本地倉庫已經包含了全部遠端倉庫的 commits,因此此次 push
不會再失敗:
➜ test-marketing-testcase git:(master) git push Counting objects: 5, done. Delta compression using up to 4 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (5/5), 516 bytes | 0 bytes/s, done. Total 5 (delta 2), reused 0 (delta 0) To git@git.souche-inc.com:testGroup/test-marketing-testcase.git bc77233..b0ce5b6 master -> master
這樣,就把 push
時本地倉庫和遠端倉庫內容衝突的問題解決了。
原文參考:https://juejin.im/book/5a124b29f265da431d3c472e/section/5a124c1f6fb9a0450b65fd3e