Git 解決合並分支時的沖突


參考鏈接:https://www.liaoxuefeng.com/wiki/896043488029600/900004111093344

創建分支時,新分支的文件內容建立在原分支的基礎上,我們稱這時候的文件狀態為A,當兩個分支上都對A之前的文件狀態進行修改后,再去合並就會出現沖突(我大概是這樣理解的)比如參考連接上廖老師舉的例子:

新建一個分支,修改readme.txt的最后一行,然后添加,提交,切換到master分支,再修改readme.txt最后一行為與另一個分支不同的狀態.然后修改,提交

在這種情況下,Git無法執行“快速合並”,只能試圖把各自的修改合並起來,但這種合並就可能會有沖突,我們試試看:

$ git merge feature1#將指定的分支(feature1)與當前分支合並
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt#conflict:沖突
Automatic merge failed; fix conflicts and then commit the result.

  git status也可以告訴我們沖突的文件:

$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

	both modified:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")#沒有 改動 被添加到commit

  我們可以直接查看readme.txt的內容:

$cat readme.txt
#這四行是相同的內容
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
<<<<<<< HEAD(從下一行開始到"====="為HEAD分支的內容
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature1

  手動修改並保存,不論是修改那個分支的,都需要將這個A狀態之前的改為一致,然后再提交

用帶參數的git log也可以看到分支的合並情況:(像不像把上面的圖給豎起來)

最后刪除feature分支,工作就完成了.

小結:

當Git無法自動合並分支時,就必須首先解決沖突。解決沖突后,再提交,合並完成。

解決沖突就是把Git合並失敗的文件手動編輯為我們希望的內容,再提交。

git log --graph命令可以看到分支合並圖。

 

 

當我們切換到一個分支,Git會自動提示我們此分支內的commit 與遠程分支的數量:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM