Learn to Resolve GitHub Merge Conflicts: A Step-by-Step Guide

Learn to Resolve GitHub Merge Conflicts: A Step-by-Step Guide

In this article, I'm going to explain to you what are GitHub merge conflicts and how to resolve them, and also some points which you should keep in mind to save yourself from getting conflicts.

What are GitHub merge conflicts?

A merge conflict is very straightforward to understand when people make different changes to the same line of the same file on different branches, so GitHub gets confused between those changes and is unable to decide which changes it should keep and which ones should be removed. There are too many ways to resolve these conflicts, let's continue...

How to resolve these conflicts?

Handling a merge conflict becomes easy once you understand what a GitHub merge conflict is.

To resolve the conflicts, you will have to decide which changes you want to keep in your code and which ones should be removed, to resolve the conlicts you can use a code editor like VS Code or GitHub's default editor.

Using GitHub's default editor

  1. Click on the Resolve conflicts button

  2. GitHub uses branch names and symbols <<<<<<<, =======, >>>>>>> to separate both of the changes, Now decide whether you want to keep changes of your branch or the other branch, after deciding to delete the conflict makers or conflict symbols along with the code you want you to remove

  3. After accepting the changes you want to keep, the final code should look like this

  4. Finally, click on Mark as resolved button in the top-right corner

Using Visual studio code

By using visual studio code or vs code, it becomes a game of clicks to resolve merge conflicts, vs code provides you a couple of options which includs the followings -

  • Accept Current Change

    Select this when you want to keep the changes you've made

  • Accept Incoming Change

    Select this when you want to add incoming changes and decline the changes you've made

  • Accept Both Changes

    This is just an addition of the above options, select this when you want to keep changes that you've made and the incoming changes

  • Compare Changes

    When you click on this, VS code will split your screen into 2 parts, and now you can see all of the changes and decide what to keep and what should be removed

    Let's go!

  1. Open the file you have conflicts in

  1. Based on the above description, decide which changes you want to keep in your code and which ones should be removed

  2. Now, delete the other block of code along with conflict makers or symbols, your final code should look like this

What are the things you should keep in mind while resolving conflicts?

Although we have learned to resolve conflicts, it's still important to know what things you should NOT do while resolving conflicts:

  1. Don't ignore the conflict

    Ignoring a conflict can cause bigger issues in the future. It's important to address conflicts as soon as possible to prevent them from becoming more complicated.

  2. Don't automatically choose one side of the conflict

    Choosing one side of the conflict without carefully reviewing both changes can lead to unintended consequences. Be sure to carefully review both changes and consider how they will affect the overall project.

  3. Don't close the Pull request

    If you're contributing to any repository, do NOT close the pull request to resolve conflicts, instead, try out the above steps.

  4. Don't make changes outside of the conflicted area

    When resolving a conflict, only make changes to the conflicted area. Making changes outside of the conflicted area can cause further conflicts and make the resolution process more complicated

  5. Test your changes

    After resolving a conflict, it's important to test your changes to ensure that they work as expected. Failing to test your changes can lead to bugs and other issues down the line.

  6. Commit your changes

    After resolving conflicts, now it's time to commit your changes.

Conclusion

There are several ways to resolve conflicts, including using Git's built-in merge tools and third-party tools like VS Code. Both options can be effective, depending on your personal preference and the complexity of the conflict.

To read more on resolving conflicts, you can refer to the following articles

  1. Resolving merge conflicts with GitHub

  2. Resolving merge conflicts with CLI

Thank you.