Resolving Merge Conflicts in Git

Today we will look at how to resolve a merge conflict if one should happen to arise when merging two branches.

Example Git History

Suppose we have a Git version history of a single file called 99_bottles.txt that looks like this:

Here are the contents of the two different versions of 99_bottles.txt:

On most recent master commit:

On most recent alternate commit:

Merging alternate into master

Assuming we’re checked out to the master branch

We can attempt to merge alternate into our currently checked out branch, master, by using the git merge <branch-name> command.

As indicated by the terminal output, the merge failed because there was a merge conflict.

Resolving a Merge Conflict

Opening 99_bottles.txt reveals the result of the merge conflict that we must resolve before trying to merge again.

Now suppose that we wanted to keep the changes of the branch that was merged in, then we can remove the conflicted change associated with HEAD and keep the change associated with alternate to result in the following file:

Next step is to stage the file and commit:

Our second merge attempt should have been successful and resulted in the following Git history:

Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store