Synopsis
git gc [--auto] [--aggressive] [--prune=<date>]
Description
The git gc command runs housekeeping tasks: it packs loose objects, removes unreachable objects past the prune window, packs refs, and updates auxiliary files. Git automatically runs gc --auto after operations that create many loose objects (like a large fetch), so most users never invoke gc manually. Manual invocation is useful after rewriting history, deleting many branches, or to reclaim disk space immediately.
The --aggressive flag does extra work — recomputing deltas — but is rarely worth the time. Modern Git's default behavior is well-tuned. The --prune argument controls how old unreachable objects must be before they're deleted; the default is two weeks.
In day-to-day use, git gc 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 gc --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 gc 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 gc's side effects helps avoid all three.
Common Options
| Option | Description |
|---|---|
--auto | Run only if Git's heuristics say maintenance is needed. |
--aggressive | Spend more time finding optimal deltas. |
--prune=<date> | Prune unreachable objects older than this. |
--no-prune | Skip pruning of loose objects. |
--quiet | Suppress progress output. |
--force | Run even if another gc is in progress. |
Examples
git gc
# Routine maintenance
git gc --auto
# What Git runs in the background; safe to call manually
git gc --prune=now
# Aggressively reclaim space; deletes recent unreachables (use with care)
git gc --aggressive
# Slow, exhaustive repacking — rarely worth it
Common Mistakes
--prune=now immediately deletes unreachable objects, possibly including ones still referenced by reflog or stash. Don't use it casually. Disabling automatic gc on a large repo lets loose objects accumulate; configure maintenance instead for newer Git.
Related Commands
git maintenance, git repack, git prune, git fsck