Dup Ver Goto 📝

Pruning Old Commits

PT2/devtools/git git does not exist
To
35 lines, 299 words, 1777 chars Page 'PruneOldCommits' does not exist.

Sometimes I want to create a clone of a repo but with all old commits removed. This answer came from this reddit.

git checkout --orphan newmaster master~5
git commit -m "New base commit rolling up 10 years of history"
git cherry-pick master~5..master

At this point, you should have 6 commits on the newmaster branch. The root commit will have rollup of all the history, and then the five commits you wanted to keep.

If you're happy with what you have on newmaster, you can rename that to master:

git branch -mf master

You may have many other refs in the repo that refer to old commits, in which case those commits will remain. You will need to remove those refs. Once there are no refs to the old commits, you can clean up the repo:

git gc --prune=now

You will need to force push master to any remotes, but those remotes are still going to have the old refs you deleted locally so you're going to have to delete those refs on the remotes too (git push -d is a starting point). You also need to be careful that people with the old repo checked out do not push it all back again accidentally. You may not need to worry about garbage collection on the remotes as it may happen automatically after 30 days or so. Even if it does not, fresh clones of master should not get the old commits.

I'd be trying this in a throwaway clone first and checking that the result is what you want before releasing to the world.