How to Resolve Git Conflict

Photo by Maha Khairy on Unsplash

How to Resolve Git Conflict

Merge conflicts and why they occur.

Introduction

You’ve started using git and GitHub and you have started collaborating with your team on several projects.

There is the master branch where everyone has forked from and everyone has worked on the different features assigned to them and they work then for some reason, someone else changes the same file as you in the same git repository and then you both try to merge the branch into the master file, then boom, you get the error flashed at you, MERGE CONFLICT.

Merge conflicts happen when two or more people change the same file in a Git repository. Git can’t automatically merge the changes and needs you to fix the conflict.

Merge conflicts can happen when two people change the same line of code or when one person deletes a file that another person has changed.

To avoid merge conflicts, make sure you and your team members are working on different parts of the code. You can also use Git’s branching feature to work on different parts of the code and merge changes back into the main branch when you’re done.

Types of merge conflicts that can occur

When two people change the same line of code in different ways, Git can’t automatically merge the changes and needs you to fix the conflict. This type of conflict is called a “content conflict.”

To resolve a content conflict, you need to manually edit the file and choose which changes to keep.

Another type of conflict happens when one person deletes a file that another person has changed. This type of conflict is called a “file-level conflict.”

To resolve a file-level conflict, you need to decide whether to keep the deleted file or the modified file.

Overview of Git’s built-in merge tool and how it can be used to resolve conflicts

Git’s built-in merge tool is called “git mergetool.” It’s a graphical tool that helps you resolve conflicts by showing you the differences between the two versions of the file. Here’s how to use git mergetool to resolve a conflict in the terminal:

  1. Open your terminal and navigate to the repository with the merge conflict.

  2. Run the following command: git mergetool Git will open the merge tool and display the conflicted file.

  3. The merge tool will show you three versions of the file: the original version, the modified version, and the conflicted version. The conflicted version will have conflict markers that look like this:

<<<<<<< HEAD
This is the modified version of the file.
=======
This is the original version of the file.
>>>>>>> original-branch
  1. Edit the file as necessary to resolve the conflict. You can choose to keep one version of the file or combine elements from both versions. For example, you might decide to keep the modified version of a function but add some changes from the original version.

  2. Save your changes and close the merge tool.

How to use a text editor like vimdiff to resolve conflicts

You can also use a text editor like vimdiff to resolve conflicts. Here’s how to use vimdiff to resolve a conflict in the terminal:

  1. Open your terminal and navigate to the repository with the merge conflict.

  2. Run the following command: git mergetool --tool=vimdiff. Vimdiff will open and display the conflicted file.

  3. The top half of the screen will show you the modified version of the file, and the bottom half of the screen will show you the original version of the file.

  4. The lines that are different between the two versions will be highlighted in different colors. The modified version will be on the left side of the screen, and the original version will be on the right side of the screen.

  5. To resolve a conflict, move your cursor to the line you want to edit and press “Enter.” Vimdiff will split the screen into three parts: the modified version, the original version, and a blank area in between.

  6. Edit the file as necessary to resolve the conflict. You can choose to keep one version of the file or combine elements from both versions.

  7. Save your changes and close vimdiff

How to resolve conflicts using GitHub’s web interface

If you’re using GitHub to manage your Git repository, you can also resolve conflicts using GitHub’s web interface. Here’s how to resolve a conflict using GitHub:

  1. Go to your repository on GitHub and click on “Pull requests.”

  2. Find the pull request with a merge conflict that you’d like to resolve and click on it.

  3. Near the bottom of your pull request, click “Resolve conflicts.”

  4. GitHub will show you the conflicted file and allow you to edit it as necessary.

  5. The conflicted file will have conflict markers that look like this:

<<<<<<< HEAD
This is the modified version of the file.
=======
This is the original version of the file.
>>>>>>> original-branch
  1. Edit the file as necessary to resolve the conflict. You can choose to keep one version of the file or combine elements from both versions.

  2. When you’re done editing the file, click “Mark as resolved.”

  3. If there are more conflicts in the pull request, repeat steps 5-7 for each conflict.

  4. Once all conflicts are resolved, click “Commit merge.”

Avoiding merge conflicts in the future

Wouldn't it be wonderful not to have to go through the stress of trying to resolve a conflict? The first time I did have to do that, it turned out to be a whole productive day spent fixing something I could have avoided.

Well, soon after that hard lesson, I came up with some tips to avoid you also making such mistakes and save yourself productive time that could be used in actually coding.

  1. Pull frequently: Make sure to pull changes from the remote repository frequently to keep your local repository up-to-date. This will help you avoid conflicts that arise from changes made by other contributors.

  2. Use feature branches: Create a new branch for each feature or bug fix that you’re working on. This will help you keep your changes separate from the main branch and reduce the risk of conflicts.

  3. Keep commits small and focused: Try to keep your commits small and focused on a single change. This will make it easier to review your changes and reduce the risk of conflicts.

  4. Communicate with other contributors: If you’re working on a project with other contributors, make sure to communicate with them about the changes you’re making. This will help you avoid conflicts that arise from conflicting changes.

  5. Use a code review process: Consider using a code review process to review changes before they’re merged into the main branch. This can help identify potential conflicts early on and reduce the risk of conflicts.