Git Conflict Tutorial

Reading Time 4 minutes

Git conflicts are just about one of the most frustrating things which can cause even the best of the best to pull their hair out. In this tutorial we will go through Git Conflict, we will see how they occur and actually self inflict a conflict through a simple scenario. We will naturally see how we can resolve the conflict also.

1. Git Conflict – Making sure our master branch is ready

First let’s make sure there is a file on the master branch with some very simple text in a text file:

test01.txt contents:
TheTestRoom is back
Please subscribe and share

2. Create branches which will eventually result in a conflict

From the master branch lets create a branch to practice resolving conflicts on:

git checkout -b conflict-branch

From the conflict-branch lets create two branches:

git branch conflict-branch-clean
git branch conflict-branch-trouble

Now lets checkout conflict-branch-clean and make some changes to one of the text files:

git checkout conflict-branch-clean
vi testfolder01/test01.txt

And change the text on the first line:

TheTestRoom is back online
Please subscribe and share

Now let’s go ahead and add and commit the file:

git add .
git commit -m "Changing TheTestRoom text to say it is online"

Now lets merge this branch into our main conflict-branch:

git checkout conflict-branch
git rebase conflict-branch-clean

Finally if you now do a git log on conflict-branch then you should see the log from conflict-branch-clean but more importantly you will also see that conflict-branch is ahead by one commit when compared to conflict-branch-trouble. We might as well also delete the conflict-branch-clean branch.

git branch -D conflict-branch-clean

3. Make a change to the same file but on conflict-branch-trouble

Lets make a change to the same file and on the same line:

git checkout conflict-branch-trouble
vi testfolder01/test01.txt

And change the text on the first line:

TheTestRoom is online
Please subscribe and share

No let’s go ahead and add and commit the file:

git add .
git commit -m "Changing TheTestRoom text to say it is online and removing back"

Now let’s try to merge this into conflict-branch:

git checkout conflict-branch
git rebase conflict-branch-trouble

And there should now be a conflict:

First, rewinding head to replay your work on top of it...
Applying: Changing TheTestRoom text to say it is online
Using index info to reconstruct a base tree...
M       testFolder01/test01.txt
Falling back to patching base and 3-way merge...
Auto-merging testFolder01/test01.txt
CONFLICT (content): Merge conflict in testFolder01/test01.txt
error: Failed to merge in the changes.
Patch failed at 0001 Changing TheTestRoom text to say it is online
The copy of the patch that failed is found in: .git/rebase-apply/patch

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".

This Git conflict has occurred because the same file has been changed multiple times and on the same line so when you try to do a merge git is confused as to which version to keep.

4. Resolving conflicts

When you do a git rebase and merge files from one branch to another, if they result in conflicts then they go into the local staging area as modified files. Go ahead and do a git status to see that the conflicted file is modified. Go ahead and vi into the file to see something like this:

<<<<<<< HEAD
TheTestRoom is online
=======
TheTestRoom is back online
>>>>>>> Changing TheTestRoom text to say it is online
Please subscribe and share

The following lines state the changes between the two versions of the same file. When looking through changes between the <<< HEAD and >>>>, the changes between HEAD and ==== are the changes which you are tring to copy from the conflict-branch-trouble where as the changes below === are the changes which are currently on the branch i.e. the changes which we merged from conflict-branch-clean.

To resolve this all we have to do is pick a version between the two and remove all the other lines and then save the file. Lets pick the version which is already on the current branch i.e. the change which was merged from conflict-branch-clean:

TheTestRoom is back online
Please subscribe and share

Once you make your changes to the file you will need to add the file back in:

git add .

And now finally run the following to resolve our Git Conflict:

git rebase --continue

This is essentially telling git to continue with the rebase.

Now if you perform a git log you will see both commits. The reason why there are two commits is because you actually did merge in the changes from the trouble branch but also applied the changes from the clean branch on top. This can be a little confusing to wrap your head around but once you do it once it all should make a little more sense. You should now have a better understanding of Git Conflict and how to resolve them.

git conflict

Direct Video Link

Mo

I am a passionate tester, father, husband and gamer. I love to write blogs about Software Testing and generally contribute back to the Software Testing world.

More Posts - Twitter - Facebook

Published by

Mo

I am a passionate tester, father, husband and gamer. I love to write blogs about Software Testing and generally contribute back to the Software Testing world.