DEV Community

Cover image for How to resolve a merge conflict in Git
Bruno
Bruno

Posted on

How to resolve a merge conflict in Git

You are probably here because you, like me and possibly every or almost every developer, had a merge conflict in that one feature Pull Request you you have been working on. Am I right? Then you are in the right place to learn how to tackle that merge conflict and procceed with your work.

donald duck and mickey fighting

But before we jump right into merge conflicts and how to tackle them, let's talk about the Git workflow to have a bit more context.

๐Ÿ—บ Git Workflow

The Git workflow is quite easy to understand once you begin to have more experience using it in your projects. Below you can see a great overview of a simple Git workflow. This one is specifically for GitHub, but the same approach is applied to other version control platforms such as GitLab, in case that is what you are using.

github feature workflow

The first step in a gitflow is to create a feature branch, where you will commit all the code changes you want to make in the repository. Then, you open a pull request where you will be able to track all the commits and merge after you have made sure everything looks alright and GitHub actions finishes running all the tests you have set up to be merged in the master branch.

๐Ÿ’ฅ How does a merge conflict happen?

This is the question for 1.000.000$ ๐Ÿค‘ A merge conflict happens when we make changes to the codebase and another developer does so too, and we want to merge their changes to our branch, but they create a conflict because there have been changes to the same file or line of code. Letโ€™s simulate a simple case scenario first and then explain how to actually solve a merge conflict. It will surely be clearer after you have seen how a merge conflict actually happens.

Start a Git repository

The first step is to create a Git repository. You can do so by running the following command:

git init
Enter fullscreen mode Exit fullscreen mode

Create a file to make changes on

Let's create a simple vanilla JavaScript file as an example and make changes there.

index.js

alert("Hello from the main branch!")
Enter fullscreen mode Exit fullscreen mode

Then run the following commands to push the code to your repository:

git add . // this will stage the changes you have made to your file
Enter fullscreen mode Exit fullscreen mode
git commit -m "Made my first changes on the main branch"
Enter fullscreen mode Exit fullscreen mode
git push
Enter fullscreen mode Exit fullscreen mode

Checkout to a new branch and make more changes there

Let's now checkout to the branch that will create the conflict on merge:

git checkout -b new-branch
Enter fullscreen mode Exit fullscreen mode

index.js

alert("Hello from the new branch!")
alert("I will create a merge conflict.")
Enter fullscreen mode Exit fullscreen mode

Then, git add, commit and push.

Checkout to the main branch and make new changes there

After creating the new branch where we made the changes to the file, let's checkout back to the main branch:

git checkout main
Enter fullscreen mode Exit fullscreen mode

index.js

alert("Hello from the main branch!")
alert("Now I have made a conflict!")
Enter fullscreen mode Exit fullscreen mode

Then, git add, commit and push.

Merge the new-branch

In order to create the merge conflict, run the following command:

git merge new-branch
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ฅ Boom: merge conflict

Now that you have tried to merge, you should get something similar to this warning telling you that you have a merge conflict:

merge conflict warning

๐Ÿงฐ How do you resolve a merge conflict?

stay calm

Now that we have a merge conflict and before we freak out to the point of calling the ambulance ๐Ÿšจ, you can either git merge --abort ๐Ÿ™ˆ or resolve the merge conflict ๐Ÿ˜Ž We will go with the last one. In order to resolve a merge conflict, you can either make use of the options on top of the code in the screenshot from the previous section (Accept Current Change, Accept Incoming Change, Accept Both Changes, Compare Changes), or choose the changes manually. Let's make some changes and get rid of the strange merge conflict stuff:

index.js

alert("Hello from the main branch!")
alert("I will create a merge conflict!")
Enter fullscreen mode Exit fullscreen mode

Now, run git add, commit and push the code. Then, run git status to confirm whether the conflict has been resolved.

seinfeld celebrating

You should have been able to resolve the merge conflict now ๐Ÿ‘

๐Ÿ“š Further reading

Here are some resources I would recommend you checking out in order to get more in depth with merge conflicts.

It is also recommended to practice by creating git repositories and trying to merge branches to get more used to resolving merge conflicts.

๐Ÿ‘‹ Thank you for reading

Thank you very much for reading this article! I hope it was helpful and I look forward to your thoughts on the comments and to seeing you in my next article!๐Ÿ˜‰

Top comments (2)

Collapse
 
brense profile image
Rense Bakker

My favorite merge conflict is when someone changes the parameters of a function i intend to use and then we both commit code that calls that same function on the same line, but with different parameters. Now I have to figure out exactly what they intended with the code changes and change all of my code that uses that function :(

Collapse
 
bcostaaa01 profile image
Bruno

That is a really great point and situation to be aware of as a developer! It can be quite frustrating to be on the โ€œsame pageโ€, but not reallyโ€ฆ๐ŸฅฒPerhaps we could say that merge conflicts can serve as a sort of โ€œlife saverโ€ before a no-return in such situations ๐Ÿ˜Œ Thank you for sharing your experience here, Rense!๐Ÿ™‚