Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it resulting in a merge conflict?

This was the initial snapshot of my git repository

enter image description here

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
like image 357
Holmes.Sherlock Avatar asked Dec 22 '15 12:12

Holmes.Sherlock


2 Answers

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)

enter image description here

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

[enter image description here]

enter image description here

enter image description here

like image 150
CodeWizard Avatar answered Oct 07 '22 19:10

CodeWizard


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.

like image 1
Edward Thomson Avatar answered Oct 07 '22 19:10

Edward Thomson