Why is it resulting in a merge conflict?

梦想的初衷 提交于 2019-12-04 20:53:36

问题


This was the initial snapshot of my git repository

On branch master, file m1 contains

L1

On branch dev, file m1 contains

L1
L2

If I try to merge dev from master, it results in a conflict.

$ 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

Though I didn't modify line 2 of m1 in master, how did it result in a conflict?

To verify actual contents of the file and to be sure if this is caused by white-spaces:

On branch master

git branch
  dev
* master

$ xxd m1
0000000: 4c31 0a                                  L1.

On branch dev

$ git checkout dev
Switched to branch 'dev'

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

Here's the script I used to create this 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

回答1:


The answer is that there is a conflict because there isn't any merge-base commit for the 2 branches.

Here is how to generate the problem in fewer steps.
Create orphan branch (Orphan branch is branch without any history)

You can see here that they are not sharing the same tree

[

]




回答2:


Because the common ancestor is empty.

In master you've added one line to an empty file. In the dev branch you've added two lines to an empty file.

It doesn't matter that one of the lines is in common, you have to choose which side you want to take; the side with one line or the side with two.




回答3:


This is easy to recreate:

% 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!

The issue happens because line 2 in both files is different. In your master branch, line 2 is simply an "EOF", while in the dev branch line 2 is "L2".



来源:https://stackoverflow.com/questions/34416225/why-is-it-resulting-in-a-merge-conflict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!