How to squash git commits
Tags: git, programming, tutorial, beginners
Published: 2020-06-19 13:12:28

---
title: How to squash git commits
published: true
tags: git, programming, tutorial, beginners
canonical_url: https://gitbetter.substack.com/p/how-to-squash-git-commits
---

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0830dc3-2650-43fc-bfb9-c5f4851f9c5d_1587x2245.png)

> #### This post was originally posted in the newsletter [GitBetter](https://gitbetter.substack.com/). If you are interested in leveling up your game in Git, you can subscribe to it.

Before learning how to squash commits let’s see what is git squash first.

Git squash is a technique that helps you to take a series of commits and condense it to a few commits.

For example, assume that you have a series of n commits. By squashing you can make all the n-commits to a single commit.

Squashing is mainly used to condense a large number of commits to make it to a small number of meaningful commits. So that we can make the git history clearer.

It is also used while merging branches. Most people will advise you to always squash the commits and rebase it with the parent branch (like master or develop).

So when you are squashing and rebasing with the parent branch, the history of the main branch will be very clean and will have only meaningful commits.

#### How to do git squash

For example, consider the following git history

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F61c1f0ca-09fd-4451-8302-6a9165c7b83f_582x159.png)

In that, you can see the last three commits. Those three commits explain that we have added a new file and have added some content.

It’s good to have it as a single commit that explains that the new file has been added with some content. So let’s see how to squash the last three commits to a single commit.

```
git rebase -i HEAD~3
```

git rebase -i is an interactive tool that helps you to squash commits. And it comes up with various options. But let’s discuss only git squash.

Don’t worry if you are not very comfortable with git-rebase. Squashing commit is a very simple technique to achieve with interactive git-rebase (i.e) git rebase -i

HEAD~3 explains that we are taking the last three commits.

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F871a70d9-63c1-4f6e-b0ef-597e6542669d_570x504.png)

The interactive rebase will open up the editor. And you can see how rebase -i has taken the last three commits. And note the number of options that it has.

Let’s concentrate only on squash.

As mentioned earlier, let’s make it to a single commit.

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcbf35930-28e8-4dd1-9dd9-3997e226d49a_537x192.png)

You can see that we have marked the last two commits to squash. You can use **squash** or **s** to mark the commit to squash.

In the above example, the squashed commits will be merged to the main commit (i.e) the commit marked as the pick.

After marking the commits you can save the editor.

After saving the editor, rebase -i will open another editor to enter the commit message like the below image.

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7d855260-4835-4cca-894f-bd13fdf62c26_546x481.png)

After editing your commit message you can save this editor. Note that the line starting with **#** will be ignored.

The git log as follows

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2d537f4a-cd69-4c78-aed9-79af3b6341b3_595x74.png)

I have edited the commit message and also note that we have squashed those three commits to a single commit.

And note that the commit hash has also changed. git rebase will always create a new commit with respective changes.

##### Use with caution

Remember that squashing will change the git history. So if you have already pushed the branch to remote then it is not advisable to squash.

Always squash before you push the changes to remote.

#### Using the fixup option to squash

You can also use the fixup option to squash commits. Fixup is the same as squash but it won’t allow you to edit the commit message. It will take the main commit (i.e) the commit marked as the pick as the commit message.

Let’s see an example

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd91ae5b-88df-46ac-82a1-eb16df668083_507x105.png)

You can use **fixup** or **f** to pick up the commits. After picking up the commits you can save the editor. The interactive rebase will save the commit message.

The git history is as follows

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F753c308b-647f-444d-a992-f19a941c7f1f_591x54.png)

Note that the Git has taken the main commit message and squashed the other two commits into it. And also note that the commit hash has been changed too.

#### Using squash when merging the branch

Git also provides the option to squash when you are merging a branch. This is a very useful command and comes in handy very much.

Most of us will be working in branches. We will be checking out branches to build a new feature or to fix some quick bugs.

So when we are ready to merge those changes to the main branch (master or develop) then squashing those changes will make more sense.

So consider the below command

```
git merge --squash target_branch_name
```

This command will take all the commits from the target branch, squash them, and stage all changes in the current branch. Then you can commit all the changes in a single commit.

Let’s see an example

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf1fc717-29c5-435d-86d9-9a73592eb6fc_507x391.png)

You can see that we are merging a branch to develop with —squash option. Git will take all the changes and have staged those changes.

#### Squashing commits in Github

Github also provides the option to squash commits. This is also a very helpful feature that comes in handy when you are merging a pull request.

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9cd5040-ff40-422b-a703-66f91ffb974d_878x679.png)

You can squash and merge the changes. So that all the five commits will be condensed to a single commit.

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd6a6756-a2b5-489e-ad12-aa3fb815f895_1148x215.png)

You can see that a single commit is made with a relevant commit message.


I hope you learned something :)

Thank you for reading :)

#### *If you have come this much, then I think you are much interested in Git. You can subscribe to my newsletter [GitBetter](https://gitbetter.substack.com/) to get tricks, tips, and advanced topics of Git.*