Git Reference ยท Maintenance and Recovery
git prune command
Reference for git prune: find and delete unreachable loose objects with -n, -v and --expire, why it is usually run through git gc, and how reflogs keep objects alive.
git prune removes loose objects that no reference can reach. An object is reachable if some branch, tag, remote-tracking ref, reflog entry, the index, a stash, or another protected ref leads to it through commits, trees or tags. Everything else is garbage: old commits after a reset, blobs from git add followed by a change, and so on.
git prune is a low-level plumbing command. In daily work you call git gc, which runs it for you with a safe age limit. Calling git prune directly is useful to see what is garbage (-n) or to delete it right away.
git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>...]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
| Option | Description |
|---|---|
-n, --dry-run |
Only report what would be deleted. |
-v, --verbose |
Print each object ID and its type (blob, tree, commit, tag). |
--progress |
Show progress. |
--expire <time> |
Only delete loose unreachable objects older than the time (now, 2.weeks.ago, a date). Without it, prune deletes all unreachable loose objects; git gc passes two weeks. |
<head>... |
Treat these commits as additional roots, so what they reach is kept. Useful in scripts. |
-- |
End of options. |
Output: with -v, one line per object: the ID and its type. Nothing without -v or -n.
Usage notes
- Reachability roots include all refs, the reflogs (so "lost" commits are safe for a while), the index, per-worktree refs and
HEAD, and the entries ofrefs/stash. - Loose objects only. Unreachable objects inside a packfile are not removed by
git prune; git repack with-d/-a(or--cruft) handles those, andgit gcruns both steps. - Races. Another command may be creating an object that is not yet referenced. The default two-week window in
git gcavoids deleting it. Runningprunewith--expire=nowin an active repository is risky. - Reflog first. If you want an object truly gone (a leaked secret, a huge blob), expire the reflog (
git reflog expire --expire=now --all) and then prune. Copies in other clones, forks or remote-tracking refs remain. - Recovering. Before pruning,
git fsck --lost-foundcan list dangling commits and blobs you might still want. After pruning they are gone for good. - Related command:
git prune-packedremoves loose objects that are also in a pack, andgit remote pruneis a different command that removes stale remote-tracking branches. - Worktrees:
git worktree prunecleans administrative data of deleted linked worktrees;git prunedoes not.