Merge conflicts and learn how to resolve them in git.

Merge conflicts

In this article, it gives knowledge about how merge conflicts happen and learn how to resolve merge conflicts using different methods and tools. To resolve merge conflicts we will be using tools and commands in git.

So, what is a Merge conflict? Merge Conflict is a warning when we try to merge branches if the same files are altered in both branches.
For instance, assume you’re working on an HTML file for web development. You use a master branch to have a mainline, and test branch to work without disturbing the master branch. Here, you have a file called index.html, you altered it in the test branch and commit it. You also altered the master branch’s index.html file. When you try to merge the test branch with master, it shows a conflicting message as git suggest you solve it to merge.
This is the same when two developers in a team try to alter files and merge it into the master branch. At, this time it shows merge conflict.

How to resolve a merge conflict

I have illustrated the images for reference please verify it.
Here, I have a directory called git-merge-conflict-demo with file content index.html.
Firstly initialize the git repository.

$ git init

Add the files to the git repository.

$ git add .

Commit the files to the git repository.

$ git commit -m "First commit"


Then create the branch called test.

$ git branch test

Navigate to the test branch and change the contents using the nano text editor.

$ git checkout test
$ nano index.html

Then add the file and commit to the git repository.

$ git add . 
$ git commit -m "First test commit"


Navigate to the master branch and now change the contents of the index.html file. Add it and commit it to the master branch.

$ git checkout master
$ nano index.html

Alter some contents in index.html, then add and commit it to the master branch.

$ git add . 
$ git commit -m "Altered commit in master"

Now try to merge the test branch with the master branch. It shows the conflict, auto-merge failed.

$ git merge test
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and the commit the result.

Try the status command to show unmerged files and changes. Now, try the diff command it shows the difference between the two files as the following image.

How to resolve the merge conflict

1. The direct way to resolve is to clean-up the merge working tree by manual. Abort the merge option using the command

$ git merge --abort

2. Use the git mergetool such a vimdiff, araxis, vimdiff2. mergetool are the different graphical tools that can be used to point out the difference in two files and help to resolve it. The image shows the vimdiff2 tool

$ git mergetool --tool=vimdiff2


3. Use the diff command to show the difference between the files in the MERGE_HEAD version.

$ git diff


The contents above ======= are contents in our side or current branch. The contents below to it are on the other side. The text in red is the altered content. This is the same as in the mergetool option.

4. Use Status command to show the unmerged files and change contents.

$ git status

5. Use log -- merge -p the command to show log files and changed contents.

$ git log --merge -p


Use above all these methods use them as a guide to change the content of the files and then merge the files.

Learn how to branch in git -> Go.
Click here for Git archives.

To view official git documentation please visit git docs.

As I’m new to the blog, I trying to improve the contents daily. Please support it through comments.

 1,007 total views