Squash older commits on git

Posted on December 16, 2021

If you want to get rid of old commints in your git history but keep latest commits, you can use rebase like this:

git rebase -i HEAD~n

You will the list of n commits in the past. You edit the list adding squash on any commit that you want to squash with a most recent:

pick bnt89ua commit-msg
pick 12v4c23 commit-msg
squash 317o1a6 commit-msg
squash 76ba099 commit-msg

Save the changes and exit. Git will ask to edit the new commit messages, and it will squash them.

If the list of old commits is going to be big, you can first select gedit as a default editor for easier editing, in you linux machine

git config --global core.editor "gedit -s"