This guide demonstrates how to resolve some common conflicts via the command line. You will need a text editor to resolve some of the conflicts.

Creating an edit collision

This is the most common type of conflict. It happens when two branches have changed the same part of the same file, and then those branches are merged together. For example, if you make a change on a particular line in a file, and your colleague working in a repository makes a change on the exact same line, a merge conflict occurs. Git has trouble understanding which change should be used, so it asks you to help out.

When this sort of conflict occurs, Git writes a special block into the file that contains the contents of both versions where the conflict occurred. To complete this type of merge, use your text editor to resolve the conflict, then add the file and commit it to complete the merge.

For this example, pretend you're doing somework being in branch-a, with some changes to a file called planets.md. Meanwhile, your friend on the other side of the world has made a different change to the same line. After running git status, Git informs you that there's a merge conflict:

Running git status tells you about the conflict.

git status
# # On branch branch-b
# # You have unmerged paths.
# #   (fix conflicts and run "git commit")
# #
# # Unmerged paths:
# #   (use "git add ..." to mark resolution)
# #
# # both modified:      planets.md
# #
# no changes added to commit (use "git add" and/or "git commit -a")

Both of you have modified planets.md. When you open the file in your text editor, you'll see both changes:

the number of planets are
<<<<<<< HEAD
nine
=======
eight
>>>>>>> branch-a

In branch-a, you wrote the word "nine," but your friend wrote "eight." Git automatically adds conflict markers to the affected areas. A conflict-marked area begins with <<<<<<< and ends with >>>>>>>. These are also known as the conflict markers. The two conflicting blocks themselves are divided by a =======.

You have several options here. You can either keep your changes, take your friend's changes, or make a brand new change. Whatever you do, you need to make sure to resolve the conflict such that the file makes sense, and everyone is happy.

In this case, you'll put in a whole new version of the line that reflects what happened. The goal is to get the file to look exactly how you want it. You'll delete the conflict markers, and just type out a new change:

the number of planets are
nine, or eight, depending on who you ask.

That's it! You can now commit this change with a new commit message, and be on your way.

Resolving a removed file conflict

Removed file conflicts occur when one person edits a file, and another person deletes that file in their branch. Git does not know if you want to keep the file with the new edits, or delete the file and forget about those edits. This example will show how to resolve this both ways.

Keeping the edited file

First, you'll resolve the conflict by keeping the new changes. Suppose that you added a line to your README.md in branch-b, but someone else has deleted the file entirely in branch-c. Git declares the following conflict:

# CONFLICT (modify/delete): README.md deleted in HEAD and modified in branch-b. Version branch-b of README.md left in tree.
# Automatic merge failed; fix conflicts and then commit the result.

git status
# # On branch branch-c
# # 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.md
# #
# no changes added to commit (use "git add" and/or "git commit -a")

You can resolve this by adding the file back, and committing it once more:

git add README.md

git commit
# [branch-c 9bc3b01] Merge branch 'branch-b' into branch-c

git show | head
# commit 9bc3b0130fe0178359d51243b5b882076a12f554
# Merge: 4c80a63 7e8b679
# Author: tekkub :bear:  
# Date:   Sat Jun 1 18:39:40 2013 -0700

#     Merge branch 'branch-b' into branch-c
>
#     Conflicts:
#         README.md

Resolving by removing the file

Now, you'll resolve that conflict the opposite way: by leaving the file deleted. Again, with a merge conflict, Git declares:

# CONFLICT (modify/delete): README.md deleted in HEAD and modified in branch-c. Version branch-c of README.md left in tree.
# Automatic merge failed; fix conflicts and then commit the result.

git status
# # On branch branch-d
# # 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.md
# #
# no changes added to commit (use "git add" and/or "git commit -a")

Now, you want to remove the file, so do that with the git rm command:

git rm README.md
# README.md: needs merge
# rm 'README.md'

Looks good, so commit it with the default message:

git commit
# [branch-d 6f89e49] Merge branch 'branch-c' into branch-d

git show | head
# commit 6f89e49189ba3a2b7440fc434f351cb041b3999e
# Merge: 211261b fcc1093
# Author: tekkub :bear:  
# Date:   Sat Jun 1 18:43:01 2013 -0700

#     Merge branch 'branch-c' into branch-d
>
#     Conflicts:
#         README.md