Git: clean up orphaned objects with reflog, prune and gc
What git reflog, git prune and git gc do, the gc.pruneExpire setting and git filter-repo for rewriting history.
A memo on the Git commands that clean up local history and orphaned objects: git reflog, git prune and git gc, plus the gc.pruneExpire setting. See also this article.
Commands
| Command | Effect |
|---|---|
git reflog |
History of HEAD movements |
git prune |
Removes orphaned Git objects (dangerous!) |
git gc |
Automatic cleanup + prune (also handles packs) |
Delay before orphaned objects are removed
git gc
git config --global gc.pruneExpire "90.days.ago"
gc.pruneExpire sets the minimum age an unreachable object must have to be removed by git gc; here 90 days. Newer objects are kept, which leaves time to recover them with git reflog.
Rewriting history
To really shorten or filter a repository's history, the reference tool is git filter-repo; usage examples: commandmasters.com/commands/git-filter-repo-common.
Notes
git prunepermanently deletes unreferenced objects: do not use it without checkinggit reflogfirst.- Rewriting history changes commit IDs and requires a force push on a shared repository.