Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What constitutes a merge conflict in Git?

Tags:

git

internals

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?

like image 581
Nathan Long Avatar asked Feb 07 '11 11:02

Nathan Long


People also ask

What are Git merge conflicts?

Git can handle most merges on its own with automatic merging features. A conflict arises when two separate branches have made edits to the same line in a file, or when a file has been deleted in one branch but edited in the other. Conflicts will most likely happen when working in a team environment.

How does Git determine merge conflicts?

Instead, Git uses an algorithm to determine what bits of a file have changed and if any of those bits could represent a conflict. For simple text files, Git uses an approach known as the longest common subsequence algorithm to perform merges and to detect merge conflicts.

How do you identify a merge conflict?

To see the beginning of the merge conflict in your file, search the file for the conflict marker <<<<<<< . When you open the file in your text editor, you'll see the changes from the HEAD or base branch after the line <<<<<<< HEAD .

What causes a merge conflict?

Often, merge conflicts happen when people make different changes to the same line of the same file, or when one person edits a file and another person deletes the same file. You must resolve all merge conflicts before you can merge a pull request on GitHub.


1 Answers

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.

like image 130
Simon Richter Avatar answered Oct 04 '22 13:10

Simon Richter