Synopsis
git prune [-n] [--expire=<date>]
Description
The git prune command deletes loose objects that aren't reachable from any ref. It is normally invoked indirectly by git gc; running it directly bypasses some of gc's safety checks. The --expire grace period (default 2 weeks) prevents deleting recently created objects that may belong to in-progress operations.
In day-to-day use, git prune integrates closely with shell aliases, editor plugins, and continuous integration. Power users often add aliases that combine flags they always pass, or wrap the command in scripts that enforce team conventions. Output formatting can be customized via Git config — pretty formats, color schemes, and pager behavior are all tunable. When something goes wrong, the first diagnostic step is usually to re-run the command with GIT_TRACE=1 in the environment, which reveals the underlying plumbing calls. For unusual situations, the --help output (git prune --help) opens the full manual page with details on every option, including those rarely used in casual workflows but essential for debugging or scripting at scale.
Understanding how git prune interacts with the rest of Git's data model — the object database, the index, refs, and the working tree — pays dividends. Each command operates on some subset of these pieces, and knowing which it touches helps predict outcomes and recover from mistakes. Reading the official Git documentation alongside hands-on practice in a throwaway repository is the fastest way to internalize the nuances. Most production issues with Git stem from one of three causes: surprising default behavior, partial network operations, or rewriting history that was already shared. A working mental model of git prune's side effects helps avoid all three.
When to Use
Most users never run git prune manually — git gc handles it. Reach for it only when gc's defaults don't fit, or when you've explicitly purged history with filter-repo and want immediate space reclamation. Even then, git gc --prune=now is the more common path.
Common Options
| Option | Description |
|---|---|
-n, --dry-run | Show what would be deleted without deleting. |
--expire=<date> | Only prune objects older than this. |
--progress | Show progress. |
--verbose | Detail every object pruned. |
Examples
git prune -n
# Preview prune
git prune --expire=now
# Aggressive: delete all unreachables right now
git prune --expire=2.weeks.ago
# Default behavior: keep recent unreachables
Common Mistakes
git prune --expire=now can delete objects that are still in the reflog or stash if you've recently expired them. Always run git fsck first to understand what's at risk. On a busy repo, prune while a fetch or rebase is happening can cause issues.
Related Commands
git gc, git fsck, git reflog expire, git repack