Quick recap on a three-way merge
April 28, 2023About 2 min
Quick recap on a three-way merge κ΄λ ¨
The Git Merge Handbook β Definitive Guide to Merging in Git
By reading this post, you are going to really understand git merge, one of the most common operations you'll perform in your Git repositories. Notes before we start I also created two videos covering the contents of this post. If you wish to watch a...
The Git Merge Handbook β Definitive Guide to Merging in Git
By reading this post, you are going to really understand git merge, one of the most common operations you'll perform in your Git repositories. Notes before we start I also created two videos covering the contents of this post. If you wish to watch a...
So to quickly recap, on a three-way merge, Git:
- First, locates the merge base β the common ancestor of the two branches. That is, the first commit that is reachable from both branches.
- Second, Git calculates two diffs β one diff from the merge base to the first branch, and another diff from the merge base to the second branch.
- Third, Git applies both patches to the merge base, using a 3-way merge algorithm. I haven't explained the 3-way merge yet, but I will elaborate on that later. The result is the state of the new, merge commit.
You can also understand why it's called a "3-way merge": Git merges three different states β that of the first branch, that of the second branch, and their common ancestor. In our previous example, main
, paul_branch
, and Commit 4
.
This is unlike, say, the fast-forward examples we saw before. The fast-forward examples are actually a case of a two-way merge, as Git only compares two states β for example, where main
pointed to, and where john_branch
pointed to.