Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a 3-way merge advantageous over a 2-way merge?

Wikipedia says a 3-way merge is less error-prone than a 2-way merge, and often times doesn't need user intervention. Why is this the case?

An example where a 3-way merge succeeds and a 2-way merge fails would be helpful.

like image 251
Johannes Bittner Avatar asked Nov 08 '10 23:11

Johannes Bittner


People also ask

Why is it called three-way merge?

The reason it is called a 3-way merge is because the Merge Commit is based on 3 different commits. The common ancestor of our branches, in this case commit number C3. This commit contains code before we diverge into different branches.

What is the difference between fast forward merge and 3-way merge of branches?

It is called so because, in the 3-way merge, Git uses three commits to generate the merge commit; two branch tips and their common ancestor. Typically, the fast forward merge is used by developers for small features or bug fixes while the 3-way merge is reserved for integrating longer-running features.

What is Git 3-way merge?

3-way merges use a dedicated commit to tie together the two histories. The nomenclature comes from the fact that Git uses three commits to generate the merge commit: the two branch tips and their common ancestor.

What is a merge conflict and why do they happen?

A merge conflict is an event that occurs when Git is unable to automatically resolve differences in code between two commits. When all the changes in the code occur on different lines or in different files, Git will successfully merge commits without your help.


3 Answers

Say you and your friend both checked out a file, and made some changes to it. You removed a line at the beginning, and your friend added a line at the end. Then he committed his file, and you need to merge his changes into your copy.

If you were doing a two-way merge (in other words, a diff), the tool could compare the two files, and see that the first and last lines are different. But how would it know what to do with the differences? Should the merged version include the first line? Should it include the last line?

With a three-way merge, it can compare the two files, but it can also compare each of them against the original copy (before either of you changed it). So it can see that you removed the first line, and that your friend added the last line. And it can use that information to produce the merged version.

like image 120
JW. Avatar answered Oct 19 '22 18:10

JW.


This slide from a perforce presentation is interesting:

slide image

The essential logic of a three-way merge tool is simple:

  • Compare base, source, and target files
  • Identify the "chunks" in the source and target files file:
    • Chunks that don't match the base
    • Chunks that do match the base
  • Then, put together a merged result consisting of:
    • The chunks that match one another in all 3 files
    • The chunks that don't match the base in either the source or in the target but not in both
    • The chunks that don't match the base but that do match each other (i.e., they've been changed the same way in both the source and the target)
    • Placeholders for the chunks that conflict, to be resolved by the user.

Note that the "chunks" in this illustration are purely symbolic. Each could represent lines in a file, or nodes in a hierarchy, or even files in a directory. It all depends on what a particular merge tool is capable of.

You may be asking what advantage a 3-way merge offers over a 2-way merge. Actually, there is no such thing as a two-way merge, only tools that diff two files and allow you to "merge" by picking chunks from one file or the other.
Only a 3-way merge gives you the ability to know whether or not a chunk is a change from the origin and whether or not changes conflict.

like image 115
VonC Avatar answered Oct 19 '22 17:10

VonC


A three-way merge is where two changesets to one base file are merged as they are applied, as opposed to applying one, then merging the result with the other.

For example, having two changes where a line is added in the same place could be interpreted as two additions, not a change of one line.

For example, file a has been modified by two people, one adding moose, one adding mouse.

#File a
    dog
    cat

#diff b, a
    dog
+++ mouse
    cat

#diff c, a
    dog
+++ moose
    cat

Now, if we merge the changesets as we apply them, we will get (3-way merge)

#diff b and c, a
    dog
+++ mouse
+++ moose
    cat

But if we apply b, then look at the change from b to c it will look like we are just changing a 'u' to an 'o' (2-way merge)

    #diff b, c
    dog
--- mouse
+++ moose
    cat
like image 30
Theo Belaire Avatar answered Oct 19 '22 19:10

Theo Belaire