git中什么时候合并会引发冲突

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces                                                                         
$ mkdir Test/  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces $ cd Test/  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test $ git init Initialized empty Git repository in D:/workspaces/Test/.git/  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master)

首先进入d盘下的workspaces文件夹下创建Test目录,并且将其初始化。

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master)                                                           
$ vim one.txt  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master) $ cat one.txt aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee 

然后在Test目录下,创建了one.txt文件,内容如上,然后将其加入缓存区,并且提交到本地库。

 

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master)                                                           
$ git branch hot_fix  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master) $ git branch-v git: 'branch-v' is not a git command. See 'git --help'.  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master) $ git branch -v  hot_fix 0ca2ee0 first commit * master  0ca2ee0 first commit  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master) $ git checkout hot_fix Switched to branch 'hot_fix'  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix) $ vim one.txt  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix) $ cat one.txt aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff 

创建新的分支 hot_fix 并切换到 hot_fix分支,然后修改one.txt文件,在原来的基础上增加了最后一行的内容。

注意:如果此时加入到缓存区,但没有提交到本地库时切换分支会出现错误,如果没有加入缓存区就切换分支是可以切换成功的,但是如果切换分之后,

再切换后的分支加入缓存区并且提交到本地库,那么文件的修改则会认为是切换后的分支做的。

 

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master)                                                           
$ cat one.txt aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff ggggggggggggg hhhhhhhhhhhh iiiiiiiiiiiiii jjjjjjjjjjjjj 

在master分支下,one.txt文件的内容如上所示

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix)                                                          
$ cat one.txt  aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff ggggggggggggg hhhhhhhhhhhh iiiiiiiiiiiiii 

在hot_fix分之下,one.txt文件的内容如上所示

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix)                                                          
$ git merge master Updating 0ef9a92..52dcdab Fast-forward  one.txt | 1 +  1 file changed, 1 insertion(+)  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix) $ git status On branch hot_fix nothing to commit, working tree clean  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix) $ cat one.txt aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff ggggggggggggg hhhhhhhhhhhh iiiiiiiiiiiiii jjjjjjjjjjjjj 

在hot_fix分支下,将master分支合并进hot_fix分支,可以看出,合并成功后,执行git status命令,工作树是干净的,也就是说,如果合并成功,那么将不用再执行加入缓存区,

提交到本地库等操作。并且hot_fix分支中one.txt的内容与master分支中的一样。

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix)                                                          
$ vim one.txt  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix) $ cat one.txt aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff ggggggggggggg hhhhhhhhhhhh 

在hot_fix分支下,将one.txt的最后两行删除,然后加入缓存区,提交到本地库。

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master)                                                           
$ cat one.txt  aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff ggggggggggggg hhhhhhhhhhhh iiiiiiiiiiiiii jjjjjjjjjjjjj 

在master分支下,one.txt文件中的内容依然未变。

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master)                                                           
$ git merge hot_fix Updating 52dcdab..ca43adc Fast-forward  one.txt | 2 --  1 file changed, 2 deletions(-)  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master) $ cat one.txt aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff ggggggggggggg hhhhhhhhhhhh 

在master分支下,执行合并操作,将hot_fix合并进master分支后,可以看出,master分支像hot_fix一样,one.txt的内容都少了两行。

 

 

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master)                                                           
$ cat one.txt aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff ggggggggggggg hhhhhhhhhhhhiii 

在master分支下,修改one.txt的内容,如上,再最后一行中多填了3个i,然后加入缓存区,提交到本地库。

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix)                                                          
$ cat one.txt  aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff ggggggggggggg hhhhhhhhhhhh  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix) $ git merge master Updating ca43adc..a0201e4 Fast-forward  one.txt | 2 +-  1 file changed, 1 insertion(+), 1 deletion(-)  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix) $ cat one.txt aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff ggggggggggggg hhhhhhhhhhhhiii 

在hot_fix分支下,内容仍然未变,然后合并master分支,发现更改了两行,一行为增,一行为减,git是以行为单位进行修改的,于是在hot_fix分支下,

one.txt先删去了最后一行的内容,再将master分支下新改的一整行加入进去。

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix)                                                          
$ vim one.txt  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix) $ cat one.txt ppppppppppppppppppp aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff wulawula 

在hot_fix 分支下,修改one.txt文件,删除了最后两行,并在第一行插入了多个p字母,并且又在新的最后一行加入了新字符,然后加入缓存区,提交到本地库。

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master)                                                           
$ cat one.txt aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff ggggggggggggg hhhhhhhhhhhhiii  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master) $ git merge hot_fix  Updating a0201e4..e2bde96 Fast-forward  one.txt | 5 ++---  1 file changed, 2 insertions(+), 3 deletions(-)  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master) $ cat one.txt ppppppppppppppppppp aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff wulawula 

切换到master分支,one.txt内容并未改变,然后合并hot_fix分支,是可以合并成功的,并且显示更改了五行内容,其中增加了两行内容,删除了三行内容。

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master)                                                           
$ git diff a0201e4 diff --git a/one.txt b/one.txt index c712368..4975dde 100644 --- a/one.txt +++ b/one.txt @@ -1,8 +1,7 @@ +ppppppppppppppppppp  aaaaaaaaaaaaaaaaaaaaa  bbbbbbbbbbbbbbbbbb  ccccccccccccc  ddddddddddd  eeeeeeeeeeeee -fffffffffffff -ggggggggggggg -hhhhhhhhhhhhiii +fffffffffffff wulawula

这就是具体增加的内容和删除的内容,尽管修改了多行,仍然没有发生合并冲突。

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master)                                                           
$ cat one.txt  ppppppppppppppppppp aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff wulawula last 

在master分支下,对one.txt文件进行修改,在最后一行增加了"last‘’,加入缓存区,提交到本地库。

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix)                                                          
$ cat one.txt first ppppppppppppppppppp aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff wulawula 

在hot_fix分支下,对one.txt文件进行修改,在第一行增加了"first",加入缓存区,提交到本地库。

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix)                                                          
$ git merge master Auto-merging one.txt Merge made by the 'recursive' strategy.  one.txt | 1 +  1 file changed, 1 insertion(+)  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix) $ cat one.txt first ppppppppppppppppppp aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff wulawula last 

然后合并master分支,没有发生冲突。

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master)                                                           
$ cat one.txt  ppppppppppppppppppp aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff wulawula last  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master) $ git merge hot_fix Updating d6c1af4..b8e70d9 Fast-forward  one.txt | 1 +  1 file changed, 1 insertion(+)  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master) $ cat one.txt first ppppppppppppppppppp aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff wulawula last 

切换master分支,one.txt文件的内容并未改变,然后在master分支下,合并hot_fix分支,发现也能成功,没有发生冲突。

                                                                                                                    
86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master) $ vim one.txt  86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master) $ cat one.txt ppppppppppppppppppp aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee last fffffffffffff wulawula last last 

在master分支下,修改one.txt文件,在倒数第二行和第三行增加"last",并删除第一行。增加到缓存区,提交到本地库。

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix)                                                           $ cat one.txt  first  ppppppppppppppppppp first  aaaaaaaaaaaaaaaaaaaaa fitst  bbbbbbbbbbbbbbbbbb  ccccccccccccc  ddddddddddd  eeeeeeeeeeeee  fffffffffffff wulawula 

在hot_Fix分支下,修改one.txt文件,在第二行和第三行增加"first",并删除最后一行。增加到缓存区,提交到本地库。

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix)                                                                         
$ git merge master Auto-merging one.txt CONFLICT (content): Merge conflict in one.txt Automatic merge failed; fix conflicts and then commit the result. 

合并master发生冲突。

            

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master)                                                                          
$ cat one.txt  <<<<<<< HEAD first ppppppppppppppppppp first aaaaaaaaaaaaaaaaaaaaa fitst bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee fffffffffffff wulawula ======= ppppppppppppppppppp aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee last fffffffffffff wulawula last last last >>>>>>> master 

 

在master分支下,修改one.txt文件,在最后一行增加了last。

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix)                                                                         
$ git merge master Auto-merging one.txt Merge made by the 'recursive' strategy.  one.txt | 2 +-  1 file changed, 1 insertion(+), 1 deletion(-) 

合并成功。

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (master)                                                                          
$ cat one.txt <<<<<<< HEAD first first ppppppppppppppppppp first aaaaaaaaaaaaaaaaaaaaa fitst bbbbbbbbbbbbbbbbbb ccccccccccccc edit by master ddddddddddd eeeeeeeeeeeee fffffffffffff wulawula ======= ppppppppppppppppppp aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee last fffffffffffff wulawula last last last >>>>>>> master 

在master分支下,修改one.txt文件内容,在有许多c字母的那一行添加了 edit by master。增加到缓存区,提交到本地库。

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix)                                                                         
$ cat one.txt  <<<<<<< HEAD first first ppppppppppppppppppp first aaaaaaaaaaaaaaaaaaaaa fitst bbbbbbbbbbbbbbbbbb ccccccccccccc eidt by hot_fix ddddddddddd edit by hot_fix eeeeeeeeeeeee fffffffffffff wulawula ======= ppppppppppppppppppp aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddd eeeeeeeeeeeee last fffffffffffff wulawula last last last >>>>>>> master 

在hot_fix分支下,修改one.txt文件内容,在有许多c字母的哪一行添加了 edit by hot_fix 并且在下一行也添加了相同内容。增加到缓存区,提交到本地库。

 

86155@LAPTOP-R32U2085 MINGW64 /d/workspaces/Test (hot_fix)                                                                         
$ git merge master  Auto-merging one.txt CONFLICT (content): Merge conflict in one.txt Automatic merge failed; fix conflicts and then commit the result. 

合并发生冲突。

总结:如果某一个分支在另一分支前面,那么该分支不能合并另一分支,即版本在前面的分支不能合并版本在后的分支。

如果是版本在后的分支,那么可以合并版本在前的分支,并且不管落后几个版本,都能直接合并为版本在前的样子。

如果是一个分支修改好一个版本后,另一个分支也修改好一个版本,那么合并时将会引起冲突,此时解决冲突即可。

其实引起冲突并不可怕,关键是在于能够解决冲突。

 

转载于:https://www.cnblogs.com/huangshizhou/p/10334732.html

你可能感兴趣的:(git中什么时候合并会引发冲突)