Resolving Merge Conflicts in GitHub: A Quick Guide

Download Now: Free HTML & CSS Coding Templates
Madhu Murali
Madhu Murali

Updated:

Published:

Merge conflicts in GitHub as a new user can be tricky to resolve if you don’t know what you’re doing. We’ve made things easy for you by putting together how merge conflicts can happen, a couple of different ways you can solve them, and how you can reduce merge conflicts going forward.

merge conflicts github; dinosaur munching on keyboard out of frustration that they have a merge conflict in github

Download Now: 50 Code Templates [Free Snippets]

What are Merge Conflicts?

When merging multiple branches, situations arise where you have conflicting file commits. In this scenario, Git asks you which changes you want to keep as part of the final merge.

Resolving Merge Conflicts in GitHub

There are two ways to resolve merge conflicts in GitHub, one is a simple competing line change, and the other requires solving the issue locally. Feel free to use the following jump links to navigate or just scroll.

  • Resolving Through GitHub
  • Resolving Locally

Resolving through GitHub

This is the simple way to resolve a merge conflict. An example of a competing line change would be if two developers updated one file in two separate branches. You can do this through GitHub’s conflict editor.

Step 1: Navigate to the Pull Request that needs to be resolvedimage of github pull requests screen

Step 2: Click Resolve Conflict

image points to resolve conflicts button on github

This should open up GitHub’s web editor as shown below

github web editor

Step 3: Fix the issue

Now it is up to you to resolve the merge conflict. GitHub uses ‘<<<<<<<’, ‘=======’, and ‘>>>>>>>’ as conflict markers, meaning that this denotes which part of the code is causing issues.

You can decide which part of the code you’d like to keep, if you’d like to keep both parts of the code, or if you don’t need any of the code at all. When you’re done editing the code, click ‘Mark as resolved’arrow pointing to mark as resolved button in github web editor

Then click on the green ‘commit merge’ button, which should bring you to this screen.

merge pull request screen

Click ‘Merge pull request’ and ‘commit merge’ then you should be all set to delete the feature branch.

Resolving Locally

For more complex issues, you will need to resolve the merge conflicts locally through a tool like Visual Studio Code.

Step 1: Navigate to Terminal

Here’s what a merge conflict will look like in Visual Studio Code as an example.

merge conflict in visual studio code

Step 2: Resolve the conflict in the editorresolving merge conflict by clicking ‘accept incoming’ or ‘accept combination’ or ‘accept current’

Now is where you can fix the issue through a couple of options. You can click ‘Accept incoming’ to replace the current code with the incoming code. Click ‘Accept combination’ to combine the incoming and the current code. Click ‘Accept Current’ to ignore the new code and keep the code the same

what the screen looks like after clicking accept incoming on the left side

Step 3: Commit Changes

commit changes in visual studio code in the top left

Step 4: Publish Branch

Now that you’ve resolved the merge conflict in the code, click ‘Publish Branch’ on the left side to commit your changes to the main branch.

publish branch option on left side

Here’s what the process will look like in terminal.

terminal resolving the merge conflict

Step 5: Final Result

Now head back over to GitHub, where you’ll see that the changes have been made. It’s that simple.

final result in github

Tips For Reducing Merge Conflicts

The best cure for anything is prevention. So, now that you know how to resolve merge conflicts, here are some tips to reduce the issue in the future.

Reduce the Number of Developers Working on the Same File

You may have heard the phrase “two heads are better than one,” but this isn’t always the case, especially not regarding coding. If you have multiple developers working on the same file through different branches, this will likely lead to merge conflicts. This is because two developers often have different approaches to solving the same issue.

Merge Branches More Frequently

When multiple developers are working on code, it’s inevitable that merge conflicts will happen. That’s why, it is best to merge branches as frequently as possible, that way the process should be relatively simple. If you wait until the last minute (which we’ve all been there) to merge all your branches, the issues become increasingly complex.

New Call-to-action

Related Articles

Dozens of free coding templates you can start using right now

    CMS Hub is flexible for marketers, powerful for developers, and gives customers a personalized, secure experience

    START FREE OR GET A DEMO