Three-way merging

To see how three-way merges work, first we'll create a new branch called new-feature and create a new file there called main.js. After staging and committing main.js we switch back to master. Here we'll update toc.txt and again, we'll stage and commit the changes.


Now we are in the situation described in the three-way merge scenario. Our target branch (in this case master) is one commit ahead of the commit it was when we created the objective branch (in this case new-feature). Thus, Git can't just move the master pointer to the tip of the new-feature branch because there's a commit in master that cannot be reached from new-feature.


To run a three-way merge we proceed as before and start by switching into the master branch. Now we run

git merge new-feature

VS Code will be launched where we can edit our commit message (if necessary). When we close it, Git will conclude the merge process.

Three-way merges 1
Three-way merges 2

We can see our merges in the log more clearly by using the --graph option

git log --oneline --all --graph -10

Starting from the bottom and moving up, the first merge we see is the fast-forward merge from the now-deleted bugfix branch onto master. This branch was created from commit d106b04. On it, we created commit e6614d7. Because at the time of merge master was still at commit d106b04 and we used a --no-ff option, the merge commit 4188bbb was created. Git shows this merge as a triangle where each commit is a vertex.


On the other hand, for our three-way merge, Git is displaying a trapezoid, since there's a commit on master (2aa25c1) that was created after the new-feature branch was created. Both branches merged at commit 9a4b466.