How to squash multiple commits into one with Git?

Created January 29, 2021


Git is a huge reason why the web dev community is so awesome. You can join an open-source project and add some amazing stuff to it. By committing your code and pushing it, you can share your creation with the project's creator and the open-source community. And while doing this, you are helping both yourself and the project's creator. You can learn so many new things and improve yourself as a creator!

Using Git is an essential thing that every developer should know. And learning some of the cool stuff you can do with Git will be of great help for you. For example, squashing multiple commits. If you're wondering what squash means, it simply means putting a few commits into one commit - squashing your commits.

In this quick post, we are going to learn how to squash multiple commits.

How to squash multiple commits

It is pretty easy to squash. It may look a bit weird when you're squashing a few commits for the first time, but don't worry.

In order to do squash some commits, you are going to need to run the git rebase command like this:

 git rebase -i HEAD~3

Let's explain what this command actually does. The -i in git rebase -i stands for interactive. What this does is that it opens your default text editor, like nano, in which you can edit the commands you just wrote.

With the HEAD~3 command, we say that we want the last 3 commits.

It should look something like this:

pick 149e991 Fixed bug
pick 95fce45 Fixed some more bugs
pick e448e7c Changed a typo

You can notice that in front of these commits is the word pick. What this means is that the commits with this word in front of them are going to remain in the source tree.

Checkout our latest product - the ultimate tailwindcss page creator 🚀

You need to be sure that the first commit is set to pick and the ones that you want to squash are changed from "pick" to "squash". Otherwise, you might lose some changes

And if you change pick with squash, then that commit is going to get combined with the previous one. So if we want our example to get squashed, it should look something like this:

pick 149e991 Fixed bug
squash 95fce45 Fixed some more bugs
squash e448e7c Changed a typo

Now, after doing this, you just save your file, and you are ready to push your commit.


Here is a quick demo showing you the above steps:

You can copy and paste directly from the video!

I am running this demo on a DigitalOcean Droplet, if you are new to DigitalOcean you can use this link to get $100 free credit so you could spin up your own servers.

The video was recorded with Asciinema.


As you can see, it's pretty easy to squash multiple commits. You just have to get used to it. I hope that this post helped you learn a couple of new things today and be sure to create something amazing.

If you are new to Git, make sure to check out this post on how to get started with Git:

Getting started with Git