Why does this lead to a merge conflict?

This was the first snapshot of my git repository

enter image description here

In the master branches, the m1 file contains

 L1 

In dev branches, file m1 contains

 L1 L2 

If I try to combine dev from master , this will lead to 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 

Although I did not change line 2 from m1 to master , how did this lead to a conflict?

To check the actual contents of a file and make sure that this is caused by spaces:

On the master branch

 git branch dev * master $ xxd m1 0000000: 4c31 0a L1. 

On the dev branch

 $ git checkout dev Switched to branch 'dev' $ xxd m1 0000000: 4c31 0a4c 320a L1.L2. 

Here is 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 
+6
source share
3 answers

The answer is that there is a conflict because there is no merge-base commit for 2 branches.

Here's how to create a problem in fewer steps.
Creating an orphan branch (an orphan branch is a branch without history)

enter image description here

You can see here that they do not use the same tree

[ enter image description here ]

enter image description here

enter image description here

+2
source

Since the common ancestor is empty.

In the wizard, you added one line to an empty file. In the dev branch, you added two lines to an empty file.

It does not matter that one of the lines is common, you need to choose which side you want to take; side with one line or side with two.

+1
source

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 problem arises because line 2 in both files is different. In your main branch, line 2 is simply "EOF", and in dev 2, it is "L2".

0
source

All Articles