iT邦幫忙

0

Git-如何解決合併衝突

git
WM 2019-05-11 15:31:3511942 瀏覽

衝突原因

同時修改同一個檔案的同一行code,就會發生衝突。
若修改同一個檔案的不同行code,就會成功合併。
這個邏輯很容易理解,因為同時修改同一個地方,git根本不知哪個才是正確的內容。

產生衝突

延續Git-合併分支

現在,產生一個發生衝突的情況。

建立feature分支。
https://ithelp.ithome.com.tw/upload/images/20190511/20112573wdZljIatiw.png

線圖。
https://ithelp.ithome.com.tw/upload/images/20190519/201125735ndubC4UjU.png

在master分支,將a.txt內容的第一行改為123,再commit。
https://ithelp.ithome.com.tw/upload/images/20190519/20112573UN4iWox3eS.png

線圖。
https://ithelp.ithome.com.tw/upload/images/20190519/20112573yVVvuesUqf.png

在feature分支,再將a.txt內容的第一行改為456,再commit。
https://ithelp.ithome.com.tw/upload/images/20190519/20112573ntlETmXCQx.png

線圖。
https://ithelp.ithome.com.tw/upload/images/20190519/20112573LGtergKgvQ.png

接下來,將master分支合併feature分支。
https://ithelp.ithome.com.tw/upload/images/20190511/20112573jReCdbEqPQ.png
果然發生衝突了。

解讀衝突訊息

Auto-merging a.txt:
表示git正在自動合併a.txt這個檔案。
CONFLICT (content) : Merge conflict in a.txt:
合併衝突的種類屬於內容衝突「CONFLICT (content)」,
發生衝突的檔案是a.txt「Merge conflict in a.txt」。
Automatic merge failed; fix conflicts and then commit the result.:
自動合併錯誤,請修復衝突,然後再commit結果。

看看git status的訊息
https://ithelp.ithome.com.tw/upload/images/20190519/20112573WecPQlnLrP.png
You have unmerged paths.
你有一個未合併的路徑。意味著,目前的合併尚未完成。
分支顯示(master|MERGING)也明確指示正處於合併狀態。

解決方案

git status的訊息提示,有兩個解決方式。
https://ithelp.ithome.com.tw/upload/images/20190519/201125738CEIHPM4zB.png
(fix conflicts and run "git commit")
修復衝突,再commit。
(use "git merge --abort" to abort the merge)
放棄這次的合併。

放棄合併

我們先放棄這次合併,回到合併前的狀態。
https://ithelp.ithome.com.tw/upload/images/20190511/20112573zEA8r8XBEz.png
重點注意,訊息顯示nothing to commit, working tree clean
這才是正常狀態,表示目前都沒有東西需要commit,工作目錄是乾淨的狀態。

解決衝突

再一次合併,依舊會發生衝突。
https://ithelp.ithome.com.tw/upload/images/20190511/20112573jc1xIUbnkW.png

訊息Unmerged paths:
(use "git add <file>..." to mark resolution)
git會提示你,add 衝突的檔案,標記為解決方案。
下面紅字,就代表衝突的檔案,並特別標示both modified

打開a.txt

<<<<<<< HEAD
123
=======
456
>>>>>>> feature

會發現,內容被git修改了。
這是因為遇到衝突時,git會將衝突的檔案內容改成它自訂的格式,顯示所有的修改內容。

表示目前所在分支的檔案內容。

<<<<<<< HEAD
123

表示目前feature分支的檔案內容。

456
>>>>>>> feature

中間以=======相隔

因為git不會知道,哪個才是我們要的code,預設就是全部留下來。
但,這絕不會是我們需要的,一定都是手動修改完後再合併。
假設將code修改成這樣,儲存。

123456

修改完成後,執行add,再看一次狀態。
https://ithelp.ithome.com.tw/upload/images/20190519/20112573daBsE7IFxO.png
這時訊息顯示All conflicts fixed but you are still merging.
表示衝突已解決,但注意看分支狀態,目前還處在合併中,所以仍然需要進行合併。
git有給提示:(use "git commit" to conclude merge)
可以執行git commit來結束commit。
https://ithelp.ithome.com.tw/upload/images/20190511/20112573u9kDwRLOvu.png
一樣會跳出合併訊息,儲存後離開。
https://ithelp.ithome.com.tw/upload/images/20190511/20112573tXdjYg9lhM.png

成功合併

git log
https://ithelp.ithome.com.tw/upload/images/20190519/20112573OmoWXZnNby.png

線圖。
https://ithelp.ithome.com.tw/upload/images/20190519/20112573RrS34ZgA8e.png

工作目錄是處於乾淨的狀態。
https://ithelp.ithome.com.tw/upload/images/20190519/20112573jpsiYI543c.png

本文為觀看網路教學的學習筆記。


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言