Git Conflict Tutorial

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

Git Squash Tutorial

Git Squash is a really powerful technique which we can use to combine multiple commits into one. This raises the question ‘how can someone combine multiple commits into a single commit’. Read below to find out how. To learn how to combine multiple commits into a single commit we will follow a simple example. Firstly lets create a simple branch from our main master branch and call is ‘squash’.

git checkout -b squash

We will create a branch as this will allow us to practice squashing commits. Once done we can simply throw away the branch as it wont be needed anymore. So lets go ahead and create some dummy commits. We will use ‘touch’ to create some dummy files and then commit them. So lets create a dummy file and commit it, we will repeat this three times.

touch dummy01.txt
git add .
git commit -m "Adding dummy01.txt file"

Now lets repeat this another two times.

touch dummy02.txt
git add .
git commit -m "Adding dummy02.txt file"
touch dummy03.txt
git add .
git commit -m "Adding dummy03.txt file"

Now if we perform a git log then we should see all three commits above.

commit a28d93b92249a0ebc14247bc0abb04c4c6ad40ba
Author: glassboxt 
Date:   Sun May 1 18:05:49 2016 +0100
    Adding dummy01.txt file

commit 953af0c8ac1bcd464d3c4450ed96632bb8928c0d
Author: glassboxt 
Date:   Sun Oct 25 16:34:56 2015 +0000
    Adding dummy02.txt file

commit 0773290c915c77fc89917c0882f1ad1cf387b919
Author: glassboxt 
Date:   Sun Oct 25 16:28:58 2015 +0000
    Adding dummy03.txt file

We are now in a position where we can start looking at combining these three commits into a single commit via Git Squash. The first thing we need to do is to quickly revisit ‘git rebase’ as this will help us to perform our Git squash. Git rebase allows us to make changes to the current branch in terms of combining and merging as demonstrated in this video but did you know that it can also allow us to combine multiple commits in the same branch. Lets have a look.

To combine multiple commits into a single commit we can use the ‘interactive’ command from git i.e. -i combined with Git rebase. This would look like this:

git rebase -i

The above command will allow us to use rebase in interactive mode which we can further by providing -i. Once we invoke -i we can also pass a starting and end point for the purpose of combining commits. We can for instance look at the Head and an offset commit using ~. For instance we can do this:

git rebase -i HEAD~3

What this will do is try to get all the commits in the current branch starting from the HEAD and up to commit number 3. Once you run the above you will see something similar to below:

pick 0773290 Changing test01.txt file
pick 953af0c Adding dummy01.txt file
pick a28d93b Adding dummy02.txt file
pick sdf4rgf Adding dummy03.txt file

# Rebase 035c933..a28d93b onto 035c933 (3 command(s))
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

This allows us to pick which commits to combine into a single commit. We can change the ‘pick’ option of a given commit into ‘squash’ and save our changes. Do note that this editor is similar to the way VIM works therefore we can easily close this by pressing ESC :X. Once done we should see something like this:

# This is a combination of 3 commits.
# The first commit's message is:
Adding dummy01.txt file

# This is the 2nd commit message:

Adding dummy02.txt file

# This is the 3rd commit message:

Adding dummy03.txt file

This allows us to make our name and edit the message of our final changes for the single commit. Once your happy with it you can save your changes with ‘ESC :x!’.

If you now do a git log you should see the that three commits have been combined into a single commit via Git Squash method. Congratulations as you have just learnt how to combine multiple commits into a single commit.

Git Squash Video Tutorial

git squash

Direct Video Link

Git Merge Tutorial Online

Git Merge is the concept of taking two branches, looking at the difference between the two and moving the changes from one branch and into another. The focus point of merge in git is to allow someone to carry out changes independently of anyone else ensuring that they don’t conflict with someone else’s work and then moving the changes back into a baseline branch i.e. most commonly known as the Master branch. We will also discuss why we should create a branch.

Git Merge Video Tutorial

git merge

Direct Video Link

Follow the video tutorial above to learn more about merging in git.

Git Branches Tutorial

Git branches are an amazing technique which we can use to copy whole versions of your git project and easily experiment without having to think too much about consequences. Branches are predominantly used to both experiment and work independently on changes. It allows project contributors to work in isolation without effecting changes from any other users.

Git Branches Video Tutorial

git branches

Direct Video Link

Follow my video tutorial to quickly get to grips with creating, deleting and managing branches in Git. We will also discuss the advantages of using branches to help us prevent from accidentally polluting our main code baseline.

Git Revert Tutorial

Git revert can be used to revert a file (change a file) back to a previous state. Git provides us with a powerful mechanism to change the contents of a file back to a previous version. There are many times when someone may accidentally commit a file into the project repository however it is entirely possible to revert a file back to a previous version through some simple commands.

Git Revert Video Tutorial

git revert

Direct Video Link

It is always advised to be careful when committing files into a git repository as you don’t want to spend any time reverting but note that given a mistake is made then we can revert very easily. Also note that we may want to revert due to design or principle changes.

Git Move and Delete

We can use Git Move to move and delete files. Deleting and moving files, seams simple enough right!

In this video tutorial we will see how we can delete a file and commit it to the project repository. We will also look at how to undo a delete while the commit is in staging and also further undo any changes from staging to local. Finally we will also look at how Git behaves when we want to move a file from one location to another.

Git Move and Delete Video Tutorial

git move

Direct Video Link
Follow the video tutorial above to learn more about moving and deleting files in Git.

Git Workflow Part 2

This tutorial is an extension to the last and now we will cover Workflow in greater detail. You should now have an understanding of how to commit a file into your Git Project as well a track versions of a given file.

In this tutorial we will cover more advanced topics, in particular how to see the difference between the changes of a given file. Follow the video tutorial link below.

Git Workflow Video Tutorial

git workflow

Direct Video Link
Follow this video tutorial to learn more about Git Workflow. We will take a deeper look at seeing the difference between two files across both local and staging.

Basic Git Workflow

Assuming you have been following this video series so far you should now have a Git Project all setup and ready to go as we will not cover basic Git Workflow.

The next video tutorial will be divided into two parts. The first will be about Git Workflow with the later part talking about more advanced features. Once you cover this video you should gain an understanding about making changes to a file and marking it ready for commit. Follow the video tutorial below to get started.

Basic Git Workflow Video Tutorial

basic git workflow

Direct Video Link
Follow the video tutorial to learn about the basic concept of Git Workflow.

Creating a Git Project

Now that you have setup your installation of Git as well as some basic setup we can begin thinking about actually creating a Git Project. In this video tutorial we will look at how to create a Project in Git as well as looking at some basic concepts, do and donts of Git.

Git Project Video Tutorial

git project

Direct Video Link
Follow the video tutorial above to learn more about converting a current video into a project. Converting a directory into a Project is very trivial as you only need to run a single command to do so. We will also talk about some basic configurations as part of creating the project.

Git Install and Setup

Have you ever tried to install Git and get it running but just found it too confusing or difficult to manage. Follow along in my YouTube video to see an easy real example of installing Git on a windows machine including some quick setup tips to get you up and started.

Git Install and Setup Video Tutorial

git install

Direct Video Link.

Also, follow the video series to find out more about Creating Projects, Workflow etc. We will also spend a great deal of time explaining the concepts and theory behind GIT’s rich API. This should be a very exciting journey.