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完全不同!关于这两者的差异会单独开贴!

有问题欢迎讨论!

原文地址:https://www.cnblogs.com/wangbaobao/p/7603257.html