MFH Labs : Git Merge Conflict to Master

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.

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!