What constitutes a merge conflict in Git?

7.2k views Asked by At

How does git determine that a particular merge has a conflict and what the conflict is?

My guess would go something like this: if the two commits being merged have a common parent commit, and if they have both changed line X from what the parent had, that's a conflict.

What complicates my understanding is:

  • "Changing line X" can mean replacing it with several new lines, and that's still shown as one conflict (version A has this one line, and version B has these 5 lines, or whatever)
  • If you did insert lines in one of the commits, a dumber algorithm would think that all subsequent lines had changed: Line 30 now has the former contents of line 25, 31 has the former contents of 26, etc. But git can tell that those are the same, and I don't know how.

Can anybody explain how this works, or point me to a link that does?

3

There are 3 answers

9
Simon Richter On BEST ANSWER

Basically, with git, every merge is a conflict, which leaves you with an index that contains three versions of each file, the versions from each branch and the base. On this index, various resolvers are run, which can decide for each individual file how to resolve the matter.

The first stage is a trivial resolver, which takes care of things like unchanged files, cases where one branch has modified a file while the other didn't, or where both branches contain the same new version of the file.

Afterwards, it's plugins that look at the remaining cases. There is a plugin that handles text files by identifying individual changes (like diff) in one branch and trying to apply those to the other branch, falling back on placing conflict markers if that doesn't work. You can easily hook in your own merge tool at this point, for example, you could write a tool that knows how to merge XML files without violating well-formedness, or that gives a graphical user interface that allows interactive editing and a side-by-side view (for example, kdiff3 does that).

So the presentation of conflicts is really a matter of the plugin used; the default plugin for text files will use the same style as CVS did, because people and tools are used to it, and the conflict markers are a known syntax error in almost any programming language.

0
VonC On

I don't think the merge algorithm has anything special with Git: it is a classic 3-way merge algorithm (not the Codeville one), which can be used with several strategies (default: recurse, or resolve or octopus). The result is a fairly simply merge process which is described here.
Any visualization need is then delegated to third-party merge/diff tools.

4
Shinnok On

Browse to the HOW CONFLICTS ARE PRESENTED paragraph on this page.

LE: There is no real documentation for the conflict cases nor file conflict markers and since i am getting bashed in the comments here, here's the pointers in the source code that lead somewhere close to what strategies does git follow in order to achieve a conflict state. File merge-recursive.c, search for the "CONFLICT string. By doing that we can easily find out that there are really a handful of conflict cases like:

  • CONFLICT (rename/rename)
  • CONFLICT (content)
  • CONFLICT (rename/directory)
  • CONFLICT (rename/delete)
  • CONFLICT (rename/add)
  • CONFLICT (delete/modify)
  • ... ans so on

If you ask me, yes they should be documented and clearly pointed out, but they aren't so nothing left to do then inspect the source.. but someone can really pick up from here and create a nice documentation and then send it to the git project.

@Wim Coenen yes it depends on merge strategies too, but how conflicts are presented gives much more of an insight. Then you can read merge strategies too if you ask me, but you still remain in doubt.