How to delete all Git commits except the last five

Viewed 18440

I have a very large Git repository which only contains binary files which are changed pretty often. Naturally the Git repository is much larger than the actual files in it. I don't really care about old history, I only need some of the newer history to be able to revert some wrong changes. So let's say I want to remove all commits except the last five.

Naturally I want to do this to keep the repository small so the removed commits must be purged completely from the repo.

And I want to do all this non-interactively with a single command (alias) or script. How can I do this?

2 Answers
Related