Git: Three-Way Merge

Żimuzo Obiechina
3 min readJun 1, 2020
Git logo

Teams working on a project leverage Git, as a distributed version control system, to keep track of changes made to the source code. A particularly useful feature in git is the three-way merge. This technique makes it possible to merge changes at different levels of development.

During development, multiple branches may be created from the master branch to enable an isolated working scope. For example, a feature branch may be created from the master branch. The complete development of a new feature is handled in the feature branch. This feature branch will have changes made to it, in parallel with independent changes also made to the master branch. When the feature is complete, the feature branch is then merged into the master branch.

Git merging combines a sequence of commits into one unified history of commits. Git can automatically merge commits unless there are changes that conflict in both commit sequences.

In the case of both the master and feature branch, the differences in the progress of these two branches can create merge conflicts. If these two branches both have changes to the same part of the same file, Git stops the merging process just before the merge commit so that conflicts can be manually resolved.

Running the git status command shows which files need to be resolved:

Anything that has merge conflicts and hasn’t been resolved is listed as unmerged.

Git presents visual indicators for conflicted sections that need to be resolved. For example:

This means the version in HEAD (your master branch is the top part of that block - everything above the =======), while the version in the feature branch looks like everything in the bottom part, everything below the =======.

You can resolve this conflict by replacing the entire block with this:

It has a little of both sections. Once the conflicts are fixed, the merging process can be continued using the usual stage/commit workflow, that is:

Merging is an essential process when working with Git. Check out the git docs to find out more about git add and git commit.

Thank you for reading!

--

--