Git

Git: Merge Conflicts

How to fix git conflicts

Table of Contents
  1. Reason for conflicts to happen
  2. The conflicting file
  3. Fixing the conflict

So you just tried to pull changes from a remote repository and git presents you with the message:

shell
1CONFLICT (content): Merge conflict in <filename>
2Automatic merge failed; fix conflicts and then commit the result.

From this message, you know that the automatic merge failed, that you need to fix some conflicts on some file and that you need to commit the result(or the fix).

But what does all of this mean?

Reason for conflicts to happen

Usually, a conflict happens when the project was updated but your local repository is out of sync. So when you try to push changes on a file that someone already changed, a conflicting message will show, so you can choose which version is the right version to use.

Let's say that you have the following file:

python
1# hello
2def hello():
3 """Prints Hello"""
4 print("Hello")

Then someone comes and changes the file, but since you haven't pulled the changes from the main repository, these changes won't show up in your local repository.

Then you decide to change this example function to print the traditional "Hello World". But when you try to push the changes to the main repository you get the message that a conflict happened in the hello.py file.

Let's open the file and inspect the problem.

The conflicting file

When you open the hello.py file you notice that there are some strange things in the hello function.

python
1def hello():
2<<<<<<< HEAD
3 """Prints Hello Everyone!"""
4 print("Hello Everyone!")
5=======
6 """Prints Hello World!"""
7 print("Hello World!")

At first, this might seem a bit strange, but it's quite easy to understand. Basically, git is telling you that the HEAD of the main repository is showing the following:

python
1<<<<<<< HEAD
2 """Prints Hello Everyone!"""
3 print("Hello Everyone!")

But your own repository has the following changes:

python
1=======
2 """Prints Hello World!"""
3 print("Hello World!")

As you can see, git is an amazing tool and it will tell you about issues before trying to do something. This conflict warning is asking you to check the differences between the two versions and choose which one do you want to use.

Fixing the conflict

Fixing the conflict is quite easy, all you need to do is delete the version that you don't want to use together with the <<<<<<< HEAD and ======= markers. In the hello.py file you know that we should be printing "Hello World!" as per tradition, so you decide that your version is the correct version.

python
1def hello():
2 """Prints Hello World!"""
3 print("Hello World!")

The hello.py will look like this when you fix the conflict. Now you can commit the changes and git will continue with the merge/pull command.

Webmentions

0 Like 0 Comment

You might also like these

How to ignore a file globally without having to add it every single time to your .gitignore file.

Read More
Git

Gitignore a file globally

Gitignore a file globally

Learn about git rebase, pulling changes from a master branch and how to keep your git history under control.

Read More
Git

Git: Commit history

Git: Commit history

Deleting files doesn't mean deleted permanently, learn how to turn back time and recover deleted files.

Read More
Git

Git: Recover Deleted Files

Git: Recover Deleted Files

It can be a bit painful to write full git commands over and over again, so I've created a few aliases to shorten the number of keys I have to press while doing git stuff.

Read More
Git

Git Aliases

Git Aliases