Reputation: 347
I was wondering if some of you have faced this issue.
I have a txt file in my main branch. I added 4 lines
Line1
Line2
Line3
Line4
Then I created 2 branches from the main. feature1 and feature2.
In feature1 I changed the first line from Line1 to Line1changed.
In feature2 I changed the second line from Line2 to Line2changed.
I merged feature1 into main branch without any problem, then when I tried to merge feature2 into main branch a conflict was detected.
$ git merge feature2
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.
I would like to know why.
$ git diff
diff --cc file.txt
index cd3d4bb,bb25698..0000000
--- a/file.txt
+++ b/file.txt
@@@ -1,4 -1,4 +1,9 @@@
++<<<<<<< HEAD
+Line1changed
+Line2
++=======
+ Line1
+ Line2changed
++>>>>>>> feature2
Line3
Line4
Upvotes: 1
Views: 2160
Reputation: 45819
The default merge configuration will auto-merge if changes don't overlap. But "overlap" is defined in a way that includes adjacent lines. So for example, when you make a change to line 1, that would overlap another change on line 1 or line 2 (as well as insertions before or after line 1).
Upvotes: 3
Reputation: 522762
I am not an expert in Git's merge algorithm, but I seem to recall that there is a proximity threshold for changes to adjacent lines, which triggers a conflict to happen. To recap, first you added four new lines in the main
branch:
Line1
Line2
Line3
Line4
Then, you branched into feature1
and feature2
, editing the first line in feature1
:
Line1changed
Line2
Line3
Line4
When you merged this back into the main
branch, there was no conflict, because only the first line was changed. However, when you merged feature2
into main
, both the first and second lines had different versions, i.e.
Line1changed (feature1) Line1 (feature1)
Line2 (feature1) Line2changed (feature2)
This triggered a merge conflict, because these two lines are directly adjacent to each other. I think it would have also happened for similar changes within some number of lines apart.
But the bottom line is that merge conflicts are a fact of life in software development, in Git, and really any other VCS tool.
Upvotes: 0