Resolving Git Conflicts - A Quick and Complete Guide

Git is an amazing tool to merge different branches of code. Most of the time the changes made by you and your team are merged automatically, but from time to time, conflicts occur. In this article, you will learn how to resolve these conflicts and how to prevent them from happening.

In simple terms, a merge conflict in Git occurs when two developers change the same piece of code and the only way to resolve this conflict is through manual intervention, changing the code in question and submitting a new commit.

Conflicts can occur both when merging branches and when merging commits within the same branch. The concept seems simple. Understand below how conflicts happen and how to resolve them.

How conflicts happen

Conflicts in Git are quite common and always happen when the same file has been modified by two different versions, and these versions cannot be automatically merged. Here's how conflicts can happen:

Let's say you and your friend are working on the same branch in a remote repository of GitHub. You are working on the code for a website.

You open the file "index.html" and change the code that corresponds to the site title to "Hello World".

You save what you just did through a new local commit.

git add index.html
git commit -m "Changing website title"

Then you need to use git pull to receive the remote repository, merge the code on your machine so you can then update the remote repository with your own changes.

CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

Oops! A CONFLICT message. We know the conflict occurred in index.html, because the conflict message indicates that.

When you open the file index.html, it now contains the following text:

<html>
	<head>
<<<<<<< HEAD
		<title>Hello World</title>
=======
		<title>Welcome to the website</title>
>>>>>>> master
	</head>
	<body>
		Lorem ipsum dolor sit amet consectetur ...
	</body>
</html>

Well, it turns out that your friend submitted a change followed by a push before you, changing the same line of code in a different way. In this case, he wrote "Welcome to the website" in the title, while you, changed to "Hello World".

Git is great at merging code automatically, but in cases where two different changes are made to the same piece of code, the versioning system has no way of knowing which version is the right one. So we need to intervene manually in the code, and for that, Git creates this flag, with "<<<<<<<", "=======" and ">>>>>>>", making it explicit that there was a conflict.

Understanding Git's conflict markup

To understand the conflict syntax, you must first understand that the conflict occurs between two versions of what Git considers the same piece of code.

  1. The first being your version (HEAD), and;
  2. The second being the branch version we are trying to merge into (master, in this case).

So the markup syntax will always be as follows:

<<<<<<< HEAD
	Here's your code change
=======
	Here's your friend's code change that Git tried to merge
>>>>>>> master

How to resolve conflicts

First, we need to locate all the files where conflicts have occurred. I recommend inspecting the conflict messages after git pull as in the example below:

CONFLICT (content): Merge conflict in index.html

At the end of the message, you have the file where the conflict occurred, but if by chance you have already closed the terminal and have no indication of which files have a conflict, you can use git status and see which files were modified by the merge or do a general search in the project (depending on your code editor it can be Ctrl + Shift + F) by "<<<<<<<", or "=======".

As explained above, Git generates this markup with two versions of the same code: the version you have, and the version you are trying to merge into.

Therefore, to resolve the conflict, you must change the file so that it contains only one version. Deleting the incorrect code and removing the conflict flags. Check the example below:

<html>
	<head>
<<<<<<< HEAD
		<title>Hello World</title>
=======
		<title>Welcome to the website</title>
>>>>>>> master
	</head>
	<body>
		Lorem ipsum dolor sit amet consectetur ...
	</body>
</html>

Our goal is to leave it this way, deleting the incorrect version as well as the conflict markup:

<html>
	<head>
		<title>Hello World</title>
	</head>
	<body>
		Lorem ipsum dolor sit amet consectetur ...
	</body>
</html>

The main question here was just choosing which version of the code to delete. But there will be cases where one version is complementary to the other, so you can copy part of your colleague's code and part of your code, generating a third final version from that conflict.

There are no rules about what to keep or delete in resolving a conflict. The rule is to change the file, keeping it in its functional and correct version.

Once you have resolved the conflict, you need to add the resolution to a new commit so that your code can be saved to the remote repository.

git add index.html
git commit -m "Resolving conflicts"
git push

How to avoid Git conflicts

From time to time, it's inevitable that two people change the same piece of code, so I don't recommend avoiding conflicts at all costs, but to become familiar with resolving conflicts and to adopt this as part of the development routine.

But there are cases where conflicts can create a mess, especially in large projects, where hundreds of files can be changed automatically in certain cases.

To do that, my advice is try to make everyone on the team always up-to-date with the remote branch through frequent git pull, because whenever someone has a very outdated code, that person can commit new changes to code that is 20 versions past.

This usually generates rework and headache for those who have to resolve conflicts and interpret what would be the correct version of that code. Here are some other conflict resolution tips and tools that can help you!

Tips and tools to make conflict resolution easy

If the conflict appeared during a merge, but could be avoided, you may want to undo the merge. For this, you can use the command below, which undoes the merge and goes back to the branch version before the merge and before the conflict(s) occurred.

git merge --abort

If you want to undo changes to a specific file that is causing conflict, and simply discard your local changes so that the conflict does not occur, you can use the git reset command.

git reset

To check the status of changed files, as well as what was changed in each file, two commands are useful. The status command shows the status of all the changed files in your current branch.

git status

The git diff command shows all local changes, line by line, in the modified files, including files that have conflicts and have not yet been added to the last commit.

git diff

The log command followed by the merge parameter shows a list of commits that had conflicts when merging two branches.

git log --merge

Did you find this helpful?

Ricardo Metring

Ricardo Metring

Full stack web developer and co-founder at Criar.io.
I've worked for over 10 years with software development. I'm constantly curious and learning.

Related articles