31

Let's say I have a file with this content in master:

Line 1
Line 2
Line 3
Line 4

Now say I create and checkout a new branch called test. In this branch I change the file to this:

Line 1
Line 2
Line 3 Modified
Line 4

and I commit this and switch back to master. In master I change the file to:

Line 1
Line 2
Line 3
Line 4 Modified

and I commit. Now if I merge branch test into master, I get a conflict.

Why can't git auto resolve this, using the common ancestor? If I tell git to edit conflicts using BeyondCompare as the difftool, BeyondCompare autoresolves this without even telling the user, since this isn't a real conflict. Is there a way to get git to autoresolve these? I've tried the recursive and resolve merge strategies but neither do it.

It's an issue in our company because there are certain files where multiple developers change lines in close proximity and this causes many unnecessary conflicts when they pull.

Antwane
  • 20,760
  • 7
  • 51
  • 84
Falconne
  • 582
  • 5
  • 12
  • Does this answer your question? [when exactly does a git merge conflict arise](https://stackoverflow.com/questions/42693608/when-exactly-does-a-git-merge-conflict-arise) – mkrieger1 Jun 18 '23 at 14:57

3 Answers3

25

The reason that Git behaves like this is explained well in the answers to this question:

https://softwareengineering.stackexchange.com/questions/194788/why-doesnt-git-merge-adjacent-lines-without-conflict/378258#378258

Essentially, because you need the neighboring lines to provide context to the change (you can't just use line numbers, because something may have been added or deleted above), if the lines around it have changed you usually don't want Git to just naively continue with the merge. User Arsen7 gives a good example in that thread of how this could go badly wrong.

However, I agree with you that sometimes this is quite annoying, so I wrote a custom merge driver that can resolve such conflicts during merging/rebasing. It's designed to be interactive, because I always want to check that it's going to do the right thing before going ahead, but you could easily modify it not to be if you're confident it's going to work.

If you're interested, the script is available on GitHub under a GPLv3+ license:

https://github.com/paulaltin/git-subline-merge

deltacrux
  • 1,186
  • 11
  • 18
  • 2
    Wow, that question is constructed almost exactly like mine :) I somehow didn't find it when I was searching for it before I posted mine. Thanks, I'll take a look at your script. – Falconne Sep 11 '18 at 08:44
  • 1
    No worries, hope it's useful to you. Let me know if you need help modifying it to suit your needs. – deltacrux Sep 16 '18 at 02:42
  • 2
    Also, if this answer (or any of the others) was useful, please consider upvoting and/or accepting one of them. – deltacrux Sep 16 '18 at 02:42
  • Does git-subline-merge help with appended lines? We have a file which gets entries added to it, and the ordering of additions very rarely matters (when submitted via different bugs). – Joe Casadonte Mar 07 '22 at 17:49
  • @JoeCasadonte – unfortunately it's not designed for that, it's meant to resolve unambiguous conflicts that happen to be on the same or adjacent lines. It sounds like you may be interested in [union merge](https://stackoverflow.com/a/46182410/3818946)? – deltacrux Mar 09 '22 at 21:10
1

I stumbled upon the same problem, coming from SVN I found this very weird as well.

I do not have an answer to the why, but maybe this helps:

I use another merge tool (depending on what OS you are working on), i use meld diff for solving merge conflicts (I work on linux / ubuntu).

And you can set git to use this external merge application as well...

see http://meldmerge.org/

and google for 'use meld for git'

e.g. http://meldmerge.org/help/resolving-conflicts.html

michel.iamit
  • 5,788
  • 9
  • 55
  • 74
  • 3
    Thanks for the reply. We do already use various external merge tools for git and they auto resolve these proximity conflicts on their own. However I'm looking for a way to stop git merge pausing on such a non-conflict in the first place, as it happens often enough to be annoying. – Falconne Mar 26 '15 at 19:36
  • totally agree! (I am interested in other replies / solutions...). another anoying thing is the end of line.. in case there are developers working in diff. OS.. e.g. windows and ubuntu editors think different about end of line... this cause a lot of merge conflicts. – michel.iamit Mar 26 '15 at 19:40
0

You can specify a custom merge driver that will not conflict in this case in a .gitattributes file that's in the repo.

r3m0t
  • 1,850
  • 16
  • 21