A simple way to resolve merge conflicts in GIT

merge conflicts in GIT
merge conflicts in GIT

Git is a standard repository that stores the source code for open source projects as well as private projects. 

In this tutorial, we are going to explain the simple way to resolve the merge conflicts in GIT branches. This tutorial provides some advanced steps for those newcomers who are newly started working on GIT.

What is Git Merge?

Today all the source control systems have an important feature called merging of code where multiple developers working on the same project, at the same time. And not interfering with each other’s work. GIT launched this feature to allow multiple developers for working on a branch locally then push the code to one place. Later, others can pull the code back to their local copy & started working on their own work with other collaborators.

When someone wants to bring the changes in a branch into your current branch, you use a git merge command. The merge command takes all the changes in the other branch and applies it to the current branch.

What is a merge conflict?

In every situation where work is distributed with multiple developers that time work will eventually overlap. Sometimes two developers will change the same line of code in different ways. In such a case, GIT will not able to understand which version is correct and that can only decide by the developer.

When this has happened, you will see the following error during a git merge :

Auto-merging index.txt
CONFLICT (content): Merge conflict in index.txt
Automatic merge failed; fix conflicts and then commit the result.

Now let’s solve this merge conflict issue in a simple and advanced way. 

Let’s say we have three branches master, feature1, and feature2.

[[email protected] git-merge]# git branch -a
feature1
feature2
*master

Now let’s understand the scenario here, We have one file “index.txt” which is committed from the master branch.

[[email protected] git-merge]# git status

# On branch feature

# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   index.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

Check the index.txt file,

[[email protected] git-merge]# cat index.txt
This is the first line.

Now the other developer is working on the same repository but on feature1 branch,

Checkout to feature1 branch

[[email protected] git-merge]# git checkout feature1
Switched to branch 'feature1'

Add a new line in index.txt

[[email protected] git-merge]# vim index.txt
This is the first line.
This line is added from feature1 branch.

Commit the new changes

[[email protected] git-merge]# git add .

[[email protected] git-merge]# git commit -m "This is first commit from feature1"

[feature1 1f6f5ee] This is first commit from feature1
 1 file changed, 1 insertion(+) 

Check the index.txt file

[[email protected] git-merge]# cat index.txt
This is the first line
This line is added from feature1 branch

Now one more developer added his changes in the same file “index.txt” and committed in the feature2 branch.

Checkout to feature2 branch

[[email protected] git-merge]# git checkout feature2
Switched to branch 'feature2'

Add a new line in index.txt

[[email protected] git-merge]# vim index.txt
This is the first line
This line is added from feature2 branch

Commit the new changes

[[email protected] git-merge]# git add .

[[email protected] git-merge]# git commit -m "This is first commit from feature2"

[feature2 a451d86] This is first commit from feature2

1 file changed, 1 insertion(+)

Check the index.txt file

[[email protected] git-merge]# cat index.txt
This is the first line
This line is added from feature2 branch

Now everyone is completed their work and will have to merge all the branches. 

First, we have to go to the master branch and try to merge the feature1 & feature2 branch with the master branch.

Merge feature1 branch

[[email protected] git-merge]# git checkout master
Switched to branch 'master'

[[email protected] git-merge]# git merge feature1

Updating 90d974c..1f6f5ee
Fast-forward
index.txt | 1 +
1 file changed, 1 insertion(+)

If you see the feature1 branch is successfully merged with the master branch without any conflicts.

Merge feature2 branch

[[email protected] git-merge]# git merge feature2
Auto-merging index.txt
CONFLICT (content): Merge conflict in index.txt
Automatic merge failed; fix conflicts and then commit the result.

The feature2 branch throws a merge conflict error. This is because the feature2 branch having code that is unknown to the master branch.

Now we are going to solve the merge error with the help of “mergetool”

git-mergetool – Run merge conflict resolution tools to resolve merge conflicts.

Use git mergetool to run one of several merge utilities to resolve merge conflicts. It is typically run after git merge.

SYNOPSIS

git mergetool [--tool=<tool>] [-y | --[no-]prompt] [<file>…​]

[[email protected] git-merge]# git mergetool -t vimdiff

When you hit the mergetool command it will give you a graphical window that helps you to check and resolve issues quickly.

 

git mergetool

Basically, it will show you all the history of all the commits and will need to concentrate on the red part.

Just remove the head and all the extra lines and save the file.

merge conflicts in git

Once you removed the extra lines and head from the file, let’s try to commit and merge changes to the master branch.

[[email protected] git-merge]# git add .

[[email protected] git-merge]# git commit -m "Final commit"

[master d681789] Final commit

[[email protected] git-merge]# git merge feature2

Already up-to-date.

Mergetool has fixed the conflict issue and we updated the final code in the file with vimdiff.

That’s it. We have explained “How to resolve the merge conflicts in GIT”

I hope you like the article if you find any difficulties to disable the root account then please do comment your queries or problem via the comment section, till then stay tuned to Linuxgrow.com for more such valuable articles.

Read More: How to Remove Submodule Gray Icon Folder From Git

Prashant

Welcome to Linuxgrow. I'm Prashant, a tech-blogger from Mumbai, India. I started Linuxgrow as a passion and to share my knowledge about technologies. Here at Linuxgrow, I write about Linux technologies, Aws Cloud, Wordpress blogging and scripting knowledge. You can read more about me at About us page. Thank You :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook