How can I fix & solve merge conflicts?

A merge conflict is not the end of the world. Actually, if you keep a couple of things in mind, solving conflicts is easy as pie:

  1. Keep Calm
    Above all, you need to realize that you cannot break anything: Git always allows you to go back to the state before the conflict occurred. With a simple "git merge --abort", you can always undo the merge and start over again. This makes it almost impossible to severely screw things up.

  2. How do I Know I Have a Conflict?
    When calling "git status", you'll see a special Unmerged paths category. All of the items in this category are in a conflict state and need to be dealt with:

    $ git status
    # On branch contact-form
    # You have unmerged paths.
    #   (fix conflicts and run "git commit")
    #
    # Unmerged paths:
    #   (use "git add <file>..." to mark resolution)
    #
    #       both modified:   contact.html
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    
  3. Understand When & Why a Conflict Happens
    Conflicts occur when the same file was changed in contradictory ways. Most modifications don't fall into this category: if two people just work on the same file, Git can most likely figure things out on its own.
    The most common situation when it cannot do this is when the exact same lines were edited in that file. In that case, Git has no way of knowing what's correct - you'll have to look at the changes and decide how you want the file to finally look.

  4. A Conflict is Just an Annotation
    It helps to realize that a conflict is nothing magical. In the concerned file, Git simply marks the areas that were edited in contradictory ways:

    This helps you understand which edits were made - and even on which branches.

  5. Solving Means Choosing & Editing
    Your job now is to condition the file to its desired state. There are a couple of ways to do this:

    (a) You can simply open the file in an editor, search for the conflict markers (see above image) and make any necessary modifications. When you're done, the file needs to look exactly as you want it to look.
    (b) Alternatively, you can tell Git that you'll simply go with one of the edited versions, called "ours" or "theirs".

    git checkout --ours path/to/conflict-file.css
    

    Note that there are lots of dedicated "Merge Tool" applications that help you with this process. Especially in complex situations with multiple conflicts in the same file, a good tool can be of tremendous value. We've compiled a list of merge tools in our free ebook.

  6. Wrap Up
    When you've successfully solved all conflicts, you need to do two more things:

    (1) Mark each conflicted file as solved. A simple "git add <filepath>" does this for you.
    (2) Commit the resolution just as you would commit any other change with the "git commit" command.


Tip

Solving Conflicts in Tower

In case you are using the Tower Git client, its visual Conflict Wizard will help you solve merge conflicts more easily:


Learn More

Get our Free Cheat Sheet Package

About Us

As the makers of Tower, the best Git client for Mac and Windows, we help over 80,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.

Just like with Tower, our mission with this platform is to help people become better professionals.

That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.