How To Use Git Squash
2 min read

If you’re at a branch with a lot of commits, be sure to checkout to the latest, or the actual branch.

This is the commit history. We only want to include the 3 latest commits, including the current one. Meaning we will merge these three.

The code is very simple. You have two different alternatives.

# this will take the 3 latest commits, including the current one.
> git rebase -i HEAD~3

# you can also use the commit hash before the one you want to use
> git rebase -i b1e1d4f

If you choose to use the commit hash, for example, these three will be included if you use the hash with the arrow.

Your text editor will open and look something like this. These are the 3 commits we choose.

Replace pick with squash (or s) for those you want to combine. The main one should still have pick. Save and exit.

Now, a window with all commit messages will pop up.

Comment out the commit messages of the commits you want to squash and edit the message of the one you want to persist.

Comment out all the commits you don’t want to use, and only leave the one you want. Save and exit. Done, now you can merge or rebase.

This is what the commit history looks like now: