web analytics

Git conflicts – How to resolve conflicts and merge

Sometimes when you merge two git branches, it might give you conflict. Conflict occurs because the same line in the same file has been edited differently in the two branches. This confuses git as to which change to accept and merge. Git conflicts are to be always resolved manually before git can proceed with the merge.

For example, I have two different branches here named current-branch and dev-branch. I want to merge the changes in dev-branch to current-branch now but I am getting a conflict.

[root@abc]# git branch
* current-branch

[root@abc]# git merge new-branch
Auto-merging abc.txt
CONFLICT (content): Merge conflict in abc.txt
Automatic merge failed; fix conflicts and then commit the result.

Now, to resolve the conflict, you the git mergetool

If you have not already configured the tool, you will need to configure it using any of the text editor software available in your machine as follows.

[root@abc]# git mergetool –tool-help
‘git mergetool –tool=<tool>’ may be set to one of the following:

The above command shows the options and you can choose which editor to set as the default as follows

[root@abc]# git mergetool –tool=vimdiff

The above command will open the mergetool in vimdiff editor and will show the changes to the file in both branches. HEAD refers to the current branch and the second line refers to the new-branch

Screenshot from 2021-01-27 16-06-08





Now all you need to do, is just remove the HEAD and new-branch line and decide what change to go among the two lines in different branch and save the file. Once done add the file to git again and commit it and merge should go fine.

[root@abc]# git status
On branch current-branch
Your branch is ahead of ‘origin/current-branch’ by 2 commits.
(use “git push” to publish your local commits)
All conflicts fixed but you are still merging.
(use “git commit” to conclude merge)

Changes to be committed:

modified: abc.txt

[root@abc]# git add  abc.txt

[root@abc]# git commit -m “Final file”

[root@abc]# git merge  new-branch
Already up-to-date.

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>