Understanding Git Squash

Amitkaushalofficial
5 min readFeb 20, 2023

--

What does squashing commits in git mean?

git squash combines several commits into one. The primary reason for doing this is that a lot of commit history is only relevant for the developer who generated it; so, it must be simplified before it is pushed to a shared repository.

Simply put, we use squashing to keep the branch graph clean.

Let’s understand this by and example:

In this example, we’ve squashed the commits B, C and D into E.

The Ways of Achieving Git Squash

We should note that squash is not a Git command, even if it’s a common Git operation. That is, “git squash … ” is an invalid Git command.

We’ll address two different approaches to squashing commits:

1) Interactive rebase: git rebase -i …

2) Merge with the –squash option: git merge –squash

So Let’s get started!

Squashing by Interactive Rebase

Let’s say you have made a correction to your code and committed the changes. After doing so let’s get the overview of the commits you want to squash.

To enlist the commits type below command in the terminal (Make sure you’re in the same directory/git repository you want to squash the commits in.)

git log --oneline

Here’s the output

Now Let’s squash those commits

In order to squash the commits we don’t have any explicit command like git squash or anything like that in git rather we use the rebase method to achieve the squashing of commits.

So, To squash the commits you made type in the command:

git rebase -i HEAD~N

where N is the number of commits you want to squash

I’ll be squashing 4 recent commits so the git command would be git rebase -i HEAD~4.

After we execute the command, Git will start the system default editor with the commits we want to squash and the interactive rebase help information as given below.

As we can see in the screenshot above, all four commits we want to squash are listed in the editor with the pick command.

There’s a detailed guideline on how to control each commit and commit message in the commented lines that follow.

For example, we can change the pick command of commits into s or squash to squash them:

If we save the change and exit the editor :wq, Git will do the rebase following our instructions:

On pressing enter, you’ll get a screen with the editor where you can edit your commit messages.

I’m editing my 1st commit message to (“Squashed commit”) because this will be the combined commit message.

Save and exit the editor.

Use this command to push the changes to your GitHub repository:

git push -f origin master

Squashing by Merging With the –squash Option

In some cases we make multiple commits in our feature branch while working on it. After we’ve developed the feature, we usually want to merge the feature branch to the main branch.

We want to keep the main branch graph clean, for example, one feature, one commit. But we don’t care about how many commits are in our feature branch.

In this case, we can use the commit git merge –squash command to achieve that.

Let’s understand it through an example:

As the output above shows, in this Git repository, we’ve implemented “feature commit 1” in the feature_branch branch.

In our feature_branch branch, we’ve made four commits.

Now we want to merge the result back to the main branch with one single commit to keep the main branch clean:

Unlike a regular merge, when we execute the command git merge with the –squash option, Git won’t automatically create a merge commit.

Instead, it turns all changes from the source branch (the feature_branch branch in this scenario) into local changes in the working copy:

In this example, all changes of the “feature commit 1” are about the readme.md file.

We need to commit the changes to complete the merge:

Now let’s check the branch graph:

We can see that we’ve merged all the changes in the feature branch into the main branch, and we have one single commit, 75205d0, in the main branch.

On the other hand, in the feature_branch branch, we still have four commits.

Conclusion

Git Squash is a pretty powerful tool inside Git. It allows you to basically pick up a bunch of commits and just squash them together into a big commit.

Happy Coding!

--

--

Amitkaushalofficial

I am a programmer, specializing in the app development. I have been developing apps using Java ✌😊 and Kotlin ❤❤