Squash commits in GIT

Reading Time: 4 minutes

Hi Readers, In this blog we will learn about the need and the working of squash in GIT. Git Squash is a technique which helps us to condense series of commits into single and meaningful commit.

What is squash in GIT?

While working on any repository it is very common that we do n numbers of commit. For example, suppose we are working on a task for 5 straight days, then ideally we should be having at least 5 commits in our commit history. When we are done with our work on final day, we want to merge all past commits into one large commit or we can call it as parent commit.

Why do we need to squash commits?

Consider a scenario when there are say 3 commits in a single file to complete a task and while pushing the file to source control you want only single commit to be in commit history. In that case you can merge these commit into a single parent commit and provide a description to this merged commit.

How to squash our commits?

Let’s see how we can squash our commits in our commit history. I’ll be taking a github repository for this task. Create a new branch and checkout to it.

Let’s perform some changes to the repository on this branch and then check it’s log using git log

As you can see that we are having three new commits at our new branch cicd.

Now lets try to merge these commits into one single commit. Let’s see how we can do this,

On terminal type below command,

git rebase -i HEAD~3
  • Here rebase is used to rewrite commit history in rabase to-do file. Apart from this rebase is a very useful command in git, if you want to learn more about this, do checkout here.
  • -i is used for interactive mode
  • HEAD~3 signifies to pick 3 commits from HEAD(latest commit in commit history).

This will open up a rebase todo file with details of 3 commits from HEAD. File will look like this,

Here you will need to squash the commits like this,

After successful change of this file, you will be prompt to provide message for commit, prompt will look like this,

You will need to remove existing comments of previous commits and add new commit message for merged commit like this,

After saving this file, your three commits from head would be merged into one and you will get a message on terminal of successful rebase.

You can also hit git log again to check new commit history, it would look like this,

When to use git squash?

There could be several scenarios to squash our commit. Some of the use cases are,

  • Cleaning up the commit history on local branch to make it more readable and compact.
  • While working on single file, squashing the commits to only keep last commit as the end goal.
  • To keep master branch clean with very less but meaningful commits.

That’s all for this blog, I hope you got a quick overview about how to squash commits in GIT with this blog. If you have any doubt, feel free to contact me nitin.mishra@knoldus.com.

Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and if you feel, give me suggestions on scope of improvements.

Written by 

Nitin Mishra is a Software Consultant at Knoldus Software LLP. He has done MCA from GGSIPU and completed Bachelor of Science in Computer Science from Delhi University. He is a tech enthusiast with good knowledge of Java. He is majorly focused in DevOps practice. On personal front he loves to travel mountains and writes poetry.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading