Git

Resolving Merge Conflicts in Git

Resolving Merge Conflicts in Git

The Problem

How do you resolve merge conflicts in a Git repository?

The Solution

Merge conflicts typically occur when changes are made to the same lines of the same files on different branches. This can happen on different named branches within the same repository or on branches with the same name across different remotes. When a merge conflict arises, Git will display a message similar to:

Auto-merging main.py
CONFLICT (content): Merge conflict in main.py
Automatic merge failed; fix conflicts and then commit the result.

Depending on how different the branches are, there may be multiple files with multiple conflicts per file. To resolve conflicts, you need to go through each one and instruct Git on how to handle it. Git marks conflicts by editing the relevant parts of each conflicting file like this:

<<<<<<< HEAD
print("Yo world!")
=======
print("Hi world!")
>>>>>>> otherbranch

The text below HEAD shows the content of the file on your current branch, and the text below the equals signs shows the content of the file on the branch being merged.

To resolve the conflict, delete the lines containing the branch names and the equals signs. Typically, this involves choosing the content of one branch over the other, but you can also combine changes or create a new change. As long as all merge conflict markers are removed, Git will consider the conflict resolved.

You can resolve conflicts manually by editing each conflicting file, or you can use a merge tool like vimdiff by running git mergetool. Using vimdiff, you’ll see a multi-pane view that resembles:

  ╔════════════════════╦═══════════════════════╦════════════════════╗
                                                                 
   print("Yo world!")  print("Hello world!")  print("Hi world!") 
                                                                 
  ╠════════════════════╩═══════════════════════╩════════════════════╣
                                                                   
   <<<<<<< HEAD                                                    
   print("Yo world!")                                              
   =======                                                         
   print("Hi world!")                                              
   >>>>>>> otherbranch                                             
                                                                   
                                                                   
  ╚═════════════════════════════════════════════════════════════════╝

In the top three panes, you can see the content on your current branch, the content as it was before the branches diverged, and the content on the branch being merged. The bottom pane shows the merged state and can be edited. To resolve the conflict in vimdiff, you can manually edit the bottom pane or use one of the following Vim commands:

  • :diffg LO: Choose the changes from your current branch.
  • :diffg BA: Choose the changes from before the branches diverged.
  • :diffg RE: Choose the changes from the branch being merged.

After resolving conflicts, save and exit with :wqa.

Once conflicts are resolved, whether manually in your editor or using a merge tool, you need to finalize the merge. Git automatically stages the changes for the merged files, so you only need to create a new commit:

git commit -m "Merge resolution"
Suggested Articles

Leave a Reply

Your email address will not be published. Required fields are marked *