How to Git- Resolving merge conflicts

In this tutorial, we are going to learn about Merge conflicts in Git and also how to resolve them. Merge conflict occurs while working with several people on the same part of code or file.

A Merge conflict occurs during the merge operation if two commits from different branches have changed the same content and Git cannot automatically determine how both changes should be combined when merging these branches. Git marks the conflict in the file and the programmer has to resolve the conflict manually.

 

Create a merge conflict

The following example will create a conflict during the merge operation. It assumes that repo_a and repo_b have the same origin repository defined but on different branches. We are going to change example.txt file which is inside the repository.

 

Creating Conflict 1/3
   # switch to the first directory
   cd ~/repo_a
   # make some changes to the example.txt file on repository
   # Let's stage and commit the change
   git add . 
   git commit -a -m ""
Creating Conflict 2/3
   # switch to the second directory
   cd ~/repo_b
   # make some diffrent changes to example.txt file on repository 
   # stage and commit the changes
   git add . 
   git commit -a -m "Will create conflict 2"
   # push the files to the master repository
   git push
Creating Conflict 3/3
   # switch to the first directory again
   cd ~/repo01

   # now try to push from the first directory
   # try to push --> assuming that the same remote repository is used,
   # you will get an error message
   git push

 

Result
   ! [rejected]        master -> master (fetch first)
   error: failed to push some refs to '../remote-repository.git/'
   hint: Updates were rejected because the remote contains work that you do
   hint: not have locally. This is usually caused by another repository pushing
   hint: to the same ref. You may want to first integrate the remote changes
   hint: (e.g., 'git pull ...') before pushing again.
   hint: See the 'Note about fast-forwards' in 'git push --help' for details.

 

Git also marks error message of the conflicts in the affected files. Following is the error message displayed on affected file result of merge conflict.

Message in Affected file
   <<<<<<< HEAD 
   Change in the first repository 
   ======= 
   Change in the second repository 
   >>>>>>> c24197211ddecfc230aace2240ad8b45196aaf32

 

To see the beginning of the merge conflict in your file, search the file for the conflict marker <<<<<<<. When you open the file in your text editor, you’ll see the changes from the HEAD or base branch after the line <<<<<<< HEAD. Next, you’ll see =======, which divides your changes from the changes in the other branch, followed by >>>>>>> hash_code.

In above example,  repo_a wrote to example.txt “Change in the first repository” in the base or HEAD branch and repo_a wrote to example.txt “Change in the first repository”.

 

Conflict Resolution

You need to resolve the conflict manually. To solve this, you need to integrate the remote changes into your local repository. In the following listing, the git fetchcommand gets the changes from the remote repository.

Conflict Resolution 1/2
   # get the changes via a fetch
   git fetch origin

   # now merge origin/master into the local master
   # this creates a merge conflict in your local repository
   git merge origin/master

 

Then add the affected file to the staging area and commit the result. This creates the merge commit. You can also push the integrated changes now to the remote repository.
Conflict Resolution 2/2
   # add the changed file
   git add .

   # commit the merge 
   git commit -m "Merge changes commited"
 
   # Now, push the changes to the remote repository
   git push

Git has no graphical merging tools but will accept any third-party merge tool. Meld GUI helps you compare files, directories, and version controlled projects. It provides two- and three-way comparison of both files and directories, and has support for many popular version control systems.

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...