7

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

在此处输入图像描述

在分支上master,文件m1包含

L1

在分支上dev,文件m1包含

L1
L2

如果我尝试从 合并devmaster则会导致冲突。

$ 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

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

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

在分支上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.

这是我用来创建这个 repo 的脚本。

#!/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
4

3 回答 3

3

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

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

在此处输入图像描述

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

[ 在此处输入图像描述]

在此处输入图像描述

在此处输入图像描述

于 2015-12-22T13:24:02.420 回答
1

因为共同祖先是空的。

在 master 中,您已将一行添加到一个空文件中。在 dev 分支中,您已将两行添加到一个空文件中。

有一条线是共同的没关系,你要选择你想走哪一边;有一条线的一侧或有两条线的一侧。

于 2015-12-22T15:42:31.543 回答
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 行不同。在您的 master 分支中,第 2 行只是一个“EOF”,而在 dev 分支中,第 2 行是“L2”。

于 2015-12-22T13:42:37.557 回答