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

Mo

I am a passionate tester, father, husband and gamer. I love to upload videos on my YouTube channel, write blogs about Software Testing and generally contribute back to the Software Testing world.

More Posts - Twitter - Facebook - Google Plus

Published by

Mo

I am a passionate tester, father, husband and gamer. I love to upload videos on my YouTube channel, write blogs about Software Testing and generally contribute back to the Software Testing world.

Leave a Reply