git merge 的過程及沖突處理演示


master分支上有一個1.txt文件,進行修改后提交

$ cat 1.txt
1
11
12

$ echo 13 >> 1.txt

$ git commit -a -m "modified 3.txt,add 13 in the end"
[master 4850577] modified 3.txt,add 13 in the end
 1 file changed, 1 insertion(+)

test1分支是基於未修改的1.txt創建的,切換到test1分支上,修改1.txt並提交

$ git checkout test1

$ cat 1.txt
1
11
12

$ echo 133 >> 1.txt

$ git commit -a -m "add 133 in the end"
[test1 2856268] add 133 in the end
 1 file changed, 1 insertion(+)

可見,兩次對1.txt的修改是不同的。在test1上的merge過程如下:

$ git merge master
Auto-merging 1.txt
CONFLICT (content): Merge conflict in 1.txt
Automatic merge failed; fix conflicts and then commit the result.

$ git status
On branch test1
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:   1.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ cat 1.txt
1
11
12
<<<<<<< HEAD
133
=======
13
>>>>>>> master

如上所示,系統提示了1.txt的沖突。

開始修改。

vi 1.txt #刪除沖突字符及修正沖突文本

$ cat 1.txt
1
11
12
133
13

git commit -a -m “modified 1.txt,add 133 & 13 in the end”

沖突解決結束。push即可。


這里再講一下merge后log的commit id順序問題。

merge是按照時間先后順序進行排序的。

上述merge后,commit id 排列如下:

~****** (test1)
$ git log --pretty=oneline
2ab7554fe3ba533501a4d1438f63b696a286fef4 (HEAD -> test1) modified 1.txt
28562680162334a0cb3dfe6f78d05169b3fed9af add 133 in the end
4850577884cd4df8d868fb16bf993e904f7be4c5 (master) modified 3.txt,add 13 in the end

從歷史操作可見4850577是在2856268之前的操作,所以merge后依然排列在2856268前面。

merge后的所有commit id是按照時間先后順序排列的,這個和git rebase完全不同!關於這兩者的差異會單獨開貼!

有問題歡迎討論!


免責聲明!

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



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