I recently learned that when merging two branches in git, if there are changes on two adjacent lines git declares this a conflict. For example, if file test.txt
has this content:
Line 1: A
Line 2: B
Line 3: C
Line 4: D
and in branch master
we change this to
Line 1: A
Line 2: B1
Line 3: C
Line 4: D
while in branch testing
we change this to
Line 1: A
Line 2: B
Line 3: C1
Line 4: D
and then attempt to merge testing
into master
, git declares a merge conflict. My naive expectation was that the merge would happen without conflict and yield this:
Line 1: A
Line 2: B1
Line 3: C1
Line 4: D
I am sure there is a good reason why git doesn't merge this way. Can someone explain this reason?
Best Answer
Is this git-only behavior?
After discussion with a colleague, I just tried, and SVN handles it without problem: you get the 2 lines modified.
The merge capabilities of several VCS are tested here for bazaar, darcs, git and mercurial: https://github.com/mndrix/merge-this
It seems only darcs successfully merge the "adjacent lines" case.
Applying adjacent changes to files is not a difficult problem. I really think this behavior has been chosen on-purpose.
Why would someone decide that modifying adjacent lines produces a conflict?
I would think this is to force you to look at it.
Modif number 1, on master:
Modif number 2, merged from a branch:
After merge, you don't want that:
Seeing this behavior as a feature
You can turn the git merging behavior to an advantage. When you need to keep 2 lines consistent but you can't detect it (at compilation time, early in your tests or else), you can try to join them.
Rewrite this...:
...to this:
So when you merge Modif 1...:
... with Modif 2...:
..., git will produce a conflict, and you will force you to look at it.