Conflicts

If you are dealing with multiple branches, you can start to run into situations where a change to a file in one branch actually conflicts with changes to a file in another branch. When you merge these two branches together, you will likely get conflicts that need to be fixed before your can finish the merge.

Let’s get back to our sample repository:

cd ~/my_project

And let’s create a “problematic” branch were we will place conflicting changes.

git branch problem
git branch
* master
  problem
git checkout problem
Switched to branch 'problem'

Let’s make a change to file1.txt.

echo 'THIS IS GOING TO BE A PROBLEM!' >> file1.txt
cat file1.txt
This is some text.

And here's some more text.
THIS IS GOING TO BE A PROBLEM!

Let’s stage and commit this change.

git add file1.txt
git commit -m "Making problems in file1"
[problem bc61ebd] Making problems in file1
 1 file changed, 1 insertion(+)

Now, let’s switch back to our master branch and make a change to file1.txt there.

git checkout master
Switched to branch 'master'
echo 'I want this change in file1.' >> file1.txt
cat file1.txt
This is some text.

And here's some more text.
I want this change in file1.

And let’s stage and commit this change.

git add file1.txt
git commit -m "This is an innocent change."
[master e814389] This is an innocent change.
 1 file changed, 1 insertion(+)

Now, we have 2 changes to the same file, so when we try to merge the problem branch into master, Git won’t know which commit to use, or in which order to apply the changes. So, it just fails!

git merge problem -m "Trying to merge my problem."
Auto-merging file1.txt
CONFLICT (content): Merge conflict in file1.txt
Automatic merge failed; fix conflicts and then commit the result.


Now, look at file1.txt.

cat file1.txt
This is some text.

And here's some more text.
<<<<<<< HEAD
I want this change in file1.
=======
THIS IS GOING TO BE A PROBLEM!
>>>>>>> problem

You can see that the problematic file (file1.txt) has been marked with the conflicting lines.

Also, look at what the status now is.

git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

	both modified:   file1.txt

no changes added to commit (use "git add" and/or "git commit -a")

This message is basically telling you that you are in the middle of a merge and the “Unmerged paths” are where your conflicts exist. So, let’s fix the conflicts.

In this case, I am going to change file1.txt to be what I actually want it to be.

echo "This is some text." > file1.txt
echo "" >> file1.txt
echo "And here's some more text." >> file1.txt
echo "" >> file1.txt
echo "I want this change in file1." >> file1.txt
cat file1.txt
This is some text.

And here's some more text.

I want this change in file1.

And note that the status hasn’t changed!

git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

	both modified:   file1.txt

no changes added to commit (use "git add" and/or "git commit -a")

We have to stage the changes we made and then commit them in order to finish the commit.

git add file1.txt
git commit -m "Fixing conflict in file1"
[master c93eb6a] Fixing conflict in file1
git status
On branch master
nothing to commit, working tree clean

And we’re done! Look at the commit logs and see what happened.

git log --oneline
c93eb6a (HEAD -> master) Fixing conflict in file1
e814389 This is an innocent change.
bc61ebd (problem) Making problems in file1
5b75c02 Merging feature into master.
f4dd054 Modifying file2.
cdcb791 Adding file3.
8ab55cd Modifying file1.
f5ee173 Adding file2.
52cc926 Adding file1

All of the changes are there! Even the bad ones, and the fix that made it all work.

Now that we have merged problem into master, and fixed all of the changes, let’s delete the problem branch.

git branch
* master
  problem
git branch -d problem
Deleted branch problem (was bc61ebd).