Skip to content

Squash It

Maybe you are tracking down a frustrating CI / CD issue, or debugging a tedious process that requires git commits... lots of commits. Your repository can get really busy for very little change, and this is where you can squash you git commits to clean stuff up.

How To

Warning - Make sure you commit your work—check that git status is clean. git reset --hard will throw away staged and unstaged changes. You could consider using reset --soft but yolo 😉, right?

git reset --hard HEAD~53 will squash all commits from the most current being 1 and the 52 previous ones will be wrapped up in this process.

We merge the newly combined 53 commits git merge --squash HEAD@{1} and commit them with something as easy as git commit. A better something would be formatted and nice which you could do with git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})".

Next we need to push the code back to the repository. This is what the -f force flag is really meant to do git push --force-with-lease origin main or whatever branch you are working in. When using force always define your target fully.