为什么会导致合并冲突?

时间:2015-12-22 12:45:26

标签: git git-merge git-merge-conflict

这是我的git存储库的初始快照

enter image description here

在分支master上,文件m1包含

L1

在分支dev上,文件m1包含

L1
L2

如果我尝试合并dev中的master,则会导致冲突。

$ git checkout master
Switched to branch 'master'

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

$ git diff
diff --cc m1
index 078f94b,9f46047..0000000
--- a/m1
+++ b/m1
@@@ -1,1 -1,2 +1,5 @@@
  L1
++<<<<<<< HEAD
++=======
+ L2
++>>>>>>> dev

虽然我没有修改m1master的第2行,但是它是如何导致冲突的?

验证文件的实际内容并确定是否由空格引起:

在分支master

git branch
  dev
* master

$ xxd m1
0000000: 4c31 0a                                  L1.

在分支dev

$ git checkout dev
Switched to branch 'dev'

$ xxd m1
0000000: 4c31 0a4c 320a                           L1.L2.

这是我用来创建此回购的脚本。

#!/bin/bash

mkdir git_demo
cd git_demo
git init

touch m1
git add .
git commit -m "Added file: m1"
# sleep is needed, otherwise a different repo is being created, probably because of *some* filesystem issue!
sleep 1

git branch dev
echo L1 >> m1
git add .
git commit -m "Added line L1 to m1"
# sleep is needed, otherwise a different repo is being created, probably because of *some* filesystem issue!
sleep 1

git checkout dev
echo L1 >> m1
git add .
git commit -m "Added line L1 to m1"
# sleep is needed, otherwise a different repo is being created, probably because of *some* filesystem issue!
sleep 1

echo L2 >> m1
git add .
git commit -m "Added line L2 to m1"
# sleep is needed, otherwise a different repo is being created, probably because of *some* filesystem issue!

gitg --all
git checkout master
git merge dev

3 个答案:

答案 0 :(得分:2)

答案是存在冲突,因为2个分支没有merge-base提交。

以下是如何以更少的步骤生成问题 创建孤儿分支(孤儿分支是没有任何历史的分支)

enter image description here

你可以在这里看到他们没有共享同一棵树

[enter image description here]

enter image description here

enter image description here

答案 1 :(得分:1)

因为共同的祖先是空的。

在掌握中,您已在空文件中添加了一行。在开发分支中,您已将两行添加到空文件中。

其中一条线是共同的并不重要,你必须选择你想要的那条线;有一条线的边或有两条边的边。

答案 2 :(得分:0)

这很容易重新创建:

% git init                                                                                                                                                                                                        [8:33:13]
Initialized empty Git repository in /home/martin/tmp/gitte/.git/
% touch m1                                                                                                                                                                                                        [8:33:16]
% git add m1                                                                                                                                                                                                      [8:33:40]
% git commit -m "Added file: m1"                                                                                                                                                                                  [8:33:48]
[master (root-commit) 72a9740] Added file: m1
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 m1
% git checkout -b dev                                                                                                                                                                                           [8:34:05]
Switched to a new branch 'dev'
% echo L1 >> m1                                                                                                                                                                                                   [8:34:08]
% git commit -am "Added line L1 to file m1"                                                                                                                                                                     [8:34:29]
[dev b16538c] Added line L1 to file m1
 1 file changed, 1 insertion(+)
% git checkout master                                                                                                                                                                                             [8:34:33]
Switched to branch 'master'
% echo L1 >> m1                                                                                                                                                                                                   [8:34:38]
% git commit -am "Added line L1 to file m1"                                                                                                                                                                       [8:34:46]
[master 7b952c8] Added line L1 to file m1
 1 file changed, 1 insertion(+)                                                                                                                                                    [8:35:59]
HEAD is now at 7b952c8 Added line L1 to file m1
% gitk                                                                                                                                                                                                            [8:36:04]
% echo L2 >> m1                                                                                                                                                                                                                                                                                                                                                                                                                [8:36:28]
% git commit -am "Added line L2 to file m1"                                                                                                                                                                       [8:36:28]
[master f336d77] Added line L2 to file m1
 1 file changed, 1 insertion(+)
% git merge dev  # merge conflict!

问题出现了,因为两个文件中的第2行不同。在你的主分支中,第2行只是一个&#34; EOF&#34;,而在开发分支中,第2行是&#34; L2&#34;。