20180618_Git分支管裏之解決沖突
Git分支管裏之解決沖突
感謝廖雪峰老師提供的git教程:
傳送門:
https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
人生不如意之事十之八九,合並分支往往也不是一帆風順的。
準備新的feature1
分支,繼續我們的新分支開發:
$ git checkout -b feature1 Switched to a new branch ‘feature1‘ |
修改readme.txt
最後一行,改為:
|
在feature1
分支上提交:
[feature1 14096d0] AND simple 1 file changed, 1 insertion(+), 1 deletion(-) |
切換到master
分支:
(use "git push" to publish your local commits) |
Git還會自動提示我們當前master
分支比遠程的master
分支要超前1個提交。
在master
分支上把readme.txt
文件的最後一行改為:
|
提交:
$
$
|
現在,master
分支和feature1
分支各自都分別有新的提交,變成了這樣:
這種情況下,Git無法執行“快速合並”,只能試圖把各自的修改合並起來,但這種合並就可能會有沖突,我們試試看:
$
Auto
CONFLICT Automatic |
果然沖突了!Git告訴我們,readme.txt
文件存在沖突,必須手動解決沖突後再提交。git status
也可以告訴我們沖突的文件:
|
我們可以直接查看readme.txt的內容:
<<<<<<< HEAD Creating a new branch is quick & simple. ======= Creating a new branch is quick AND simple. >>>>>>> feature1 |
Git用<<<<<<<
,=======
,>>>>>>>
標記出不同分支的內容,我們修改如下後保存:
|
再提交:
$
$
|
現在,master
分支和feature1
分支變成了下圖所示:
用帶參數的git log
也可以看到分支的合並情況:
|
最後,刪除feature1
分支:
$
Deleted |
工作完成。
小結
當Git無法自動合並分支時,就必須首先解決沖突。解決沖突後,再提交,合並完成。
解決沖突就是把Git合並失敗的文件手動編輯為我們希望的內容,再提交。
用git log --graph命令可以看到分支合並圖。
20180618_Git分支管裏之解決沖突