Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does git produce a merge conflict when lines next to each other are changed?

Tags:

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.

like image 319
Falconne Avatar asked Mar 26 '15 11:03

Falconne


People also ask

Why do I keep getting merge conflicts in Git?

Merge conflicts happen when you merge branches that have competing commits, and Git needs your help to decide which changes to incorporate in the final merge. Git can often resolve differences between branches and merge them automatically.

What is a merge conflict and why do they happen?

A merge conflict usually occurs when your current branch and the branch you want to merge into the current branch have diverged. That is, you have commits in your current branch which are not in the other branch, and vice versa. Typically, there is one branch point, which is the latest common commit.


2 Answers

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

like image 115
deltacrux Avatar answered Sep 20 '22 18:09

deltacrux


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

like image 25
michel.iamit Avatar answered Sep 19 '22 18:09

michel.iamit