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?
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.