How To Resolve Merge Conflicts In Git Pull Request (PR)?

Reading Time: 2 minutes

“How to resolve merge conflicts in Git pull request or a PR?”

This question we often ask our friends and colleagues and even Google. It can also look complex and confusing especially if you are new to Git. To resolve your queries, we will talk about it in this article and cover some important questions like “What is a merge conflict?”, “When does a merge conflict occur?”, and “How to resolve a merge conflict?”.

What Is A Merge Conflict?

We all know that Git tracks file changes. Now, let’s try to understand this with an example.

Let’s say you have a feature branch called featureA and you want to merge this feature into your master branch. Now, Git will automatically merge all the “new” pieces of code into your master branch however there will be some cases where it will not be able to do so automatically.

A merge conflict is a scenario where Git is not able to automatically merge changes as it gets confused between two different versions of code for the same file.

If you want to know more about Git, click here to read our previous articles.

How To Resolve Merge Conflicts In Git Pull Requests?

So, now that you know what is a merge conflict and when does it occur, let’s see how to fix it!

Follow these steps to resolve merge conflicts in Git pull requests:

  1. We will make sure that code on both the branches is updated with the remote. If not, first take pull of both the branches or push your local changes if any.
  2. Switch to the branch you want to merge using git checkout command.
  3. Try to merge locally like this:
git pull <the parent branch> origin 
  1. You will see an output similar to this:
Auto-merging origin_<file_name>
CONFLICT (content): Merge conflict in origin_<file_name>
Automatic merge failed; fix conflicts and then commit the result.
  1. When you open the conflicting file, you will see something like this:
int i = 10;
<<<<<< HEAD
System.out.println(i);
====== master
System.out.println("Hello!");

In this, git is telling you that the line to print “Hello!” from master branch was over-written with a different print statement in the branch. Now, you have to choose which version you want to keep. In our case, let’s choose our branch’s version.

  1. Manually resolve the conflict by editing the file keeping the content you want , something like this:
int i = 10;
System.out.println(i);
  1. The small editing we just did is considered a “change” in Git. So, now do git add, and when you do git commit , it will show you a commit message that will be autogenerated , which you can modify or just save and now push it , it will automatically update the pr that you have created.
  2. Now, you have a pr which is all ready to be reviewed and merged!

Conclusion

Well, that was it! I hope you understood what is a Git conflict and why does it occur. I am glad I could help you resolve merge conflicts in Git pull requests. You can repeat the process for every file which has a conflict. Do let me know in the comments section if you have any questions and do share this article with your friends and colleagues.


Also published on Medium.

Written by 

Mohit Saxena is a Software Consultant having experience of more than 2 years. He is always up for new challenges and loves to aggressively move towards completing the software requirements. On a personal front, he loves to climb mountains and is a big foodie.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading