How to resolve merge conflict in GIT

Merge Conflict

Often you will encounter merge conflicts while doing rebase operation as described above.

Here, is a example merge conflict error:


$ git rebase -i --onto remotes/m/release-stable  a62bcf1  remotes/xyz/dev-stable-topic

error: could not apply 0f783bb... Fixing issues with to xyz ioctls for data transfer.

When you have resolved this problem, run "git rebase --continue". 

If you prefer to skip this patch, run "git rebase --skip" instead. To check out the original branch and stop rebasing, run "git rebase --abort". 

Recorded preimage for 'abc_xyz.c' 

Could not apply 0f748bb638803d23c34a0b63e3d83f81e4d81ea9... Fixing issues with to xyz ioctls for data transfer.

The above lines above are saying the below:

  1. While applying the patch with hash 0f783bb there were merge conflicts. The headline of the patch can be seen as “Fixing issues with to xyz ioctls for data transfer.”
  2. Manually resolve the conflict.
  3. After resolving the merge conflict run the command git rebase –continue.
  4. Alternatively you can skip this patch with “git rebase –skip”.
  5. You can also, abort the whole rebase operation with “git rebase –abort”.

Merge Conflict Resolution

To handle this we will now do the below:

See which file has the conflict


$ git status 

interactive rebase in progress; onto fca9ecc 

Last command done (1 command done): pick 0f783bb Fixing issues with to xyz ioctls for data transfer.

Next commands to do (6 remaining commands):
pick 7c51caa Fixing audio broken issue
pick ef652d4 Example patch for demonstration purposes. 

(use "git rebase --edit-todo" to view and edit) 

You are currently rebasing. 

(fix conflicts and then run "git rebase --continue")
(use "git rebase --skip" to skip this patch)
(use "git rebase --abort" to check out the original branch) 

Unmerged paths:
(use "git reset HEAD ..." to unstage)
(use "git add ..." to mark resolution)
both modified: xyz_abc.c
no changes added to commit (use "git add" and/or "git commit -a") 

Most important line is the below:

both modified: xyz_abc.c

This says the file which has got into the merge conflict is xyz_abc.c. Now let’s open the file and see what the conflict is.

I prefer vim edit for viewing you can use any other edit of your choice.

Git indicates a conflict with the below marker called conflict marker:

<<<<<<< HEAD : file.c

I am a reason for merge conflict.

=======

I am another reason for merge conflict.

>>>>>>> xyzabc:file.c

The lines between <<<<<<< and ====== is what you already had locally and the lines between ======= and >>>>>>>  is what is in the remote.

Now, all you have to do is to decide which part of the code is desirable after the merge. Sometimes it maybe as simple as just removing what is already in the old file and taking the ones from the new file. For example, if I am merging file A, version X into version Y, then ideally version X should override whatever in version Y. But life is not that simple and the situation can much more worse than that. So decide what you want in the final version and modify the file accordingly. There should be no conflict marker left in the final version (once the conflict has been resolved).

Once, you have resolved the merge conflict, save and exit the VIM editor.


git add 

git commit -m "Commit message."

One thought on “How to resolve merge conflict in GIT

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s