Squash Git Commits

by Gaurav Jassal on 2014-03-14

Git is amazing and I recently learnt an awesome trick from one my colleague. If you need to combine you commits to one larger one, you can use git rebase to do that. The rebase command has some awesome options available in its —interactive (or -i) mode, and one of the most widely used is the ability to squash commits or combine commits.

For example if you want to squash last 3 commits, run this command.

Squash Git Commits

git rebase -i HEAD~3

This will take you to a log screen where you can all of you 3 commits. Now you need to change pick to squash in your commits and save it.

Squash Git Commits

In the next window you will edit your commit message.So, now you can ignore commit messages for your previous commit and add a new commit message and save. The final screen will show you that your rebase command have was completed.

Squash Git Commits

Once rebase completes you will see this screen.

Squash Git Commits

Try your logs again

git log

Squash Git Commits

You will now see that your all three commits are combined to one commit.