When merging two branches, various types of Git conflict may appear. Here you can read how to solve the most common ones.
An edit collision happens when you and your colleague are working on the same file in the repository and you both make changes to the same part of the file and then try to merge your branches together.
When git merge conflicts occur, Git writes a special block with the contents of both versions of the conflicted piece, which needs to be manually edited with a text editor, and then added back to the file and commited.
When an edit collision appears, running
git status will produce a message like this:
# On branch buddy-1# You have unmerged paths.# (fix conflicts and run "git commit")## Unmerged paths:# (use "git add ..." to mark resolution)## both modified: README.md## no changes added to commit (use "git add" and/or "git commit -a")
This means that the file that both of you have modified is README.md. Upon opening the file in the text editor, you’ll both see the extra block added by Git:
the best vegetable in the world is<<<<<<< HEADbacon=======cabbage>>>>>>> branch-meat-1
While the obvious answer is “bacon”, for some reason your co-worker has entered “cabbage” in the same spot. However, Git doesn’t know what the answer is and has marked the affectioned area with arrows, leaving your version on top and your colleague’s at the bottom.
To solve this conflict, you need to delete the markers and leave the correct version:
the best vegetable in the world isbacon
Now you can
git add the file, make a commit and carry on with the merge.
Removed file conflict
This conflict appears when one person edits the file and another one deletes it in their branch. You can decide to either keep it with the changes or discard completely.
Let’s say you’ve added a line to README.md in the branch
buddy-1, but your friend has deleted the file entirely from the branch
buddy-2. In this case, Git will produce a conflict message like this:
CONFLICT (modify/delete): README.md deleted in HEAD and modified in buddy-1. Version buddy-1 of README.md left in tree.# Automatic merge failed; fix conflicts and then commit the result.$ git status# On branch buddy-2# You have unmerged paths.# (fix conflicts and run "git commit")# Unmerged paths:# (use "git add/rm ..." as appropriate to mark resolution)# deleted by us: README.mdno changes added to commit (use "git add" and/or "git commit -a")
Keeping the edited file
If you want to keep the file, you need to add it back and commit it to the repo:
$ git add README.md$ git commit# [buddy-2 881fef7] Merge branch 'branch-1' into branch-2$ git show | head# commit 8454bf770a16b8f1ca0e7feed029d816811dd614# Merge: 29fe539 0a880f2# Author: bylek# Date: Wed Nov 4 12:37:45 2015 +0200# Merge branch 'buddy-1' into buddy-2># Conflicts:# README.md
Removing the file
If you want to remove the file you can do that with the
git rm command:
$ git rm README.md
Once you remove the file just commit it with the default message:
$ git commit[buddy-2 2ae08c4] Merge branch 'buddy-2' into buddy-1$ git show | head# commit cf867c41f29d5b672d6d46d77b39b3708e53e0a4# Merge: 211261b fe6230c# Author: bylek# Date: Wed Sep 23 12:40:51 2015 +0200# Merge branch 'buddy-1' into buddy-2># Conflicts:# README.md