Merge Conflict In Git – And How To Fix It

Git merge conflict resolution

Merge conflicts will happen sometimes when you are working with a lot of files, or a lot of other users, on a Git project. Trying to proceed with a merge and instead getting an error message can be frustrating, but merge conflicts are nothing to fear. Git makes managing and resolving merge conflicts super easy. In this article, you’ll learn what causes merge conflicts, how to resolve, how to defer them, and how to create them for practice.

What Causes a Merge Conflict?

A merge conflict appears when you try to merge a divergent branch but the merged content conflicts with existing content in the branch receiving the merge. For example, a file named “file” has conflicting content on the same line. Which content do you want to overwrite? How would you like to resolve that conflict? Git does not know how you would prefer to resolve this conflict, and therefore you are welcome to resolve it yourself and then commit your changes.

How To View Merge Conflicts

As mentioned above, it is up to you to decide how you want to resolve a conflict. But if you have a lot of files, how do you know exactly where the conflict resides? In order to move beyond a merge conflict you will need to review the text of the affected file and manually decide what goes and what stays. Git will tell you which files contain conflicts and highlights the conflicting content itself.

You will be notified of a merge conflict when trying to complete a merge. The following error message is generated when a file named “file” contains a conflict.

Auto-merging file
CONFLICT (content): Merge conflict in file
Automatic merge failed; fix conflicts and then commit the result.

Taking a cue from the information above, you can open the file called “file” and you will see a similar arrangement as below:

<<<<<<< HEAD
old line
=======
new line
>>>>>>> changes

Take a moment to analyze this text in your file. It may look like a mess, but it’s very simple when you break it down. The message contains two individual snippets of text that conflict in the same space, separated by “=====“.

In this instance, a branch called “changes” is being merged into the “master” branch, but the merge was unsuccessful because the file called “file” has conflicting content on the same line, “old line” and “new line”.

<<<<<<< HEAD indicates that the branch receiving the, referenced by “HEAD”, contains “old line”. Below, the content on the same line, “new line” is what the “changes” branch introduces.

So basically, look above the “=====” and below it to see the conflicting content and decide what to do about it.

How To Resolve Merge Conflicts

The merge action created the additional text. So you have a few options at this point. Edit the file to remove the conflict, making changes as needed, and then re-commit the finished product. If you are unprepared or unwilling to make these edits right away, you can also perform a quick Git reset to remove the conflicts and get back to the state you were in before attempting the merge.

How to Create a Merge Conflict (For Testing Purposes)

One of the best ways to learn about merge conflicts is to create one, so you can personally see how they work and how to resolve them.

You can create a merge conflict in an existing Git project, but you will probably want to create a new project. It’s easy to create a new directory and initialize a Git repository inside it.

Add some content to a blank file:

echo "new content" > file

Now add and commit this initial content:

git add -A &&
git commit -m "Initial commit"

Now, create a divergent branch:

git checkout -b new-branch

Overwrite the contents of the file:

echo "overwritten content" > file

Commit these changes:

git commit -am "made changes"

Switch back over to the master branch:

git checkout master

Stop for a moment to analyze the situation. You have a master branch with the initial contents of the file you created, and you also have a divergent branch called new-branch. You have switched back over to master and are now prepared to attempt a merge: effectively merging the contents of the divergent branch into master:

git merge new-branch

This merge should trigger the merge conflict and alert you. Now, open the file called “file” to observe the contents. You should see the breakdown of content as demonstrated above. From this point it will be easy to view the file contents from both branches in the singular file, make your edits as needed and re-commit the changes on master.


Well done! You now know how merge conflicts arise in Git and how to resolve or defer them as needed.

Let ourĀ Managed Hosting team configure your servers while you work on building your brand!

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Comments

It looks like this article doesn't have any comments yet - you can be the first. If you have any comments or questions, start the conversation!

Was this article helpful? Let us know!