git gc
Clean up and optimize the repository
The 'git gc' command (garbage collection) packs loose objects into compressed packfiles, removes objects nothing refers to any more (after their grace period), and tidies references and reflogs. Git runs a light version automatically; you rarely need it by hand. It changes storage only — never your commits, branches or files.
Synopsis
git gc [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune]Examples
Tidy up
git gcPack and clean.
Remove unreachable objects now
git gc --prune=nowAlso deletes lost commits immediately (careful).
Workflow Examples
git reflog expire --expire=now --allForget old positions
git gc --prune=now --aggressiveDelete and repack
Troubleshooting
Auto packing the repository… message
Cause: Git ran gc automatically
Nothing to do; it keeps the repository fast
Tips & Pitfalls
Best Practices
- ✓Let Git run it automatically, or schedule git maintenance.
Common Mistakes
- ✗Running git gc --prune=now while hoping to recover a lost commit — it deletes it.
FAQ
Only unreachable objects older than the grace period (2 weeks by default); reachable commits are never touched.