Git 101: How to Fix A Merge Conflict

When working with Git version control, it’s common to encounter merge conflicts, especially when you are collaborating with other people. Merge conflicts happen when Git is unable to automatically merge two branches due to conflicting changes in their files.  Say, for example, that you and a teammate both make changes to the same part of a file.  Your teammate’s changes make it into the main branch of the remote repository first, so you pull from the remote main branch to bring those changes down to your local repository.  You have changes that you made to the same part of the same file as the new content you are pulling down, and Git can’t tell if you want to keep your changes, or bring in the new ones, so it creates a merge conflict.  

Keep in mind that experiencing a merge conflict doesn’t mean that you’ve done anything incorrectly!  They are a normal part of the software development process, and knowing how to resolve them is an essential skill.  In this article, we’ll walk through the steps of manually resolving a merge conflict. 

Identify the Conflict

A screen shot of a bash terminal on MacOS, showing git version control experiencing a merge conflict between two branches.
Git experiencing a merge conflict in a zsh terminal on MacOS.

The first thing to do is identify the conflict.  In the shell, Git will tell you which files have merge conflicts.  Open those files in a text editor, like Visual Studio Code, and look for the conflict markers.  Conflict markers are groups of special characters that Git uses to identify where conflicts occur in files: <<<<<<<, =======, and >>>>>>>.

A merge conflict between two files open in the Visual Studio Code editor, showing the git-generated conflict markers.
Git-generated conflict markers in a text file; open in Visual Studio Code running on MacOS.

In a file that has a conflict, you’ll see the changes from both branches, surrounded by the conflict markers.  The changes in the current branch (the branch that you are currently on), will appear between the <<<<<<< and ======= markers, and the changes from the incoming branch (the branch you are trying to merge into your current branch) will appear between the between the ======= and >>>>>>> markers.

Note that Git will also put your repository into a “merge state” when you experience a merge conflict. (In some shells, you may see the word “merging” appear next to your branch name to signify that the repo is in a merge state.) While in the merge state, the only commands you can run will have to do with resolving the merge conflict; typically you will only be allowed to stage/unstage files, make commits, and abort the merge. If you want to quit merging without resolving the merge or making a commit, you can run the command git merge --abort to roll back the state of your repository to what it was before attempting to merge.

Resolve the Conflict

Next, you’ll need to resolve the conflict.  You’ll do this by editing the file to choose which changes you want to keep.  To keep only the changes from your current branch, delete the changes between the ======= and >>>>>>>  markers; to keep only the changes from the incoming branch, delete changes between the <<<<<<< and ======= markers.  You can also combine the changes from the current branch and changes from the incoming branch, editing the file to include changes from both branches.

When you have decided which version of the file to keep (or when you have edited the file to merge the changes together), delete any remaining conflict markers.  Double-check to make sure that the file looks correct, and then save your changes to the file.  

Do this for each file that is experiencing a merge conflict. 

Stage & Commit Changes

When you’re done, stage the modified files using the git add command, and make a commit to save your changes to the Git repository.  (Make sure to include a descriptive commit message about how you are resolving a merge conflict!) Making a commit will take your repository out of the “merge state” and allow you to pull, push, and run other commands again.

That’s all there is to it–you’ve learned how to resolve merge conflicts!  Remember, merge conflicts happen to everyone, so don’t panic when you come across them.  Take your time to understand what is causing the conflict, fix the code, and complete the merge successfully.  Happy merging! 

Add a Comment