gc Git Command Guide
The git gc command is used to run housekeeping tasks within the current repository, such as compressing file revisions, removing unreachable objects, packing refs, pruning reflog, and updating ancillary indexes like the commit-graph.
git gc Syntax:
Section titled “git gc Syntax:”git gc [--aggressive] [--auto] [--[no-]detach] [--quiet] [--prune=<date> | --no-prune] [--force] [--keep-largest-pack]Options:
Section titled “Options:”| Option | Description |
|---|---|
| —aggressive | Aggressively optimize (much slower) |
| —auto | Check if housekeeping needed, exit if not |
| —detach | Run in background (default) |
| —no-detach | Run in foreground |
| —quiet | Suppress progress reports |
| —prune= | Prune loose objects older than date (default: 2 weeks) |
| —no-prune | Don’t prune loose objects |
| —force | Force run even if another gc is running |
| —keep-largest-pack | Consolidate all except largest pack |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
| (none) | Operates on current repository |
git gc Command Samples:
Section titled “git gc Command Samples:”Basic repository maintenance
Section titled “Basic repository maintenance”git gcRun standard garbage collection and optimization.
Aggressive optimization
Section titled “Aggressive optimization”git gc --aggressiveSpend extra time for maximum optimization (not usually worth it).
Check if maintenance needed
Section titled “Check if maintenance needed”git gc --autoOnly run if repository needs housekeeping.
Prune old objects immediately
Section titled “Prune old objects immediately”git gc --prune=nowPrune loose objects regardless of age (risky if repo is active).
Force run even if another gc running
Section titled “Force run even if another gc running”git gc --forceRun garbage collection despite concurrent gc processes.
Quiet operation
Section titled “Quiet operation”git gc --quietRun without progress output.
Custom prune expiration
Section titled “Custom prune expiration”git gc --prune=1.month.agoPrune objects older than 1 month.
Keep largest pack file
Section titled “Keep largest pack file”git gc --keep-largest-packPreserve largest pack, consolidate others.
Run in foreground
Section titled “Run in foreground”git gc --no-detachRun in foreground instead of background.
Full maintenance with commit graph
Section titled “Full maintenance with commit graph”git gc && git maintenance startRun traditional gc followed by modern maintenance.
How do I run basic repository maintenance?
Section titled “How do I run basic repository maintenance?”To run basic repository maintenance, simply use:
git gcThis compresses objects, removes unreachable ones, and optimizes the repository.
How does —auto work?
Section titled “How does —auto work?”To check if maintenance is needed without forcing it, use:
git gc --autoThis exits early if no housekeeping is required based on configuration thresholds.
When should I use —aggressive?
Section titled “When should I use —aggressive?”To use aggressive optimization when optimization is critical, run:
git gc --aggressiveThis re-computes all deltas but takes significantly longer and may not be worth it.
How do I control pruning of loose objects?
Section titled “How do I control pruning of loose objects?”To control when loose objects are pruned, use —prune:
git gc --prune=1.week.agoThis prunes objects older than the specified time.
How can I run gc in the background?
Section titled “How can I run gc in the background?”To run gc in the background (default), use:
git gcOr explicitly:
git gc --detachHow do I run gc in foreground?
Section titled “How do I run gc in foreground?”To run gc in the foreground, use:
git gc --no-detachThis is useful for scripts that need gc to complete before continuing.
When is —force useful?
Section titled “When is —force useful?”To force gc even if another instance is running, use:
git gc --forceThis bypasses safety checks and should be used carefully.
How do I maintain repositories automatically?
Section titled “How do I maintain repositories automatically?”To enable automatic maintenance on regular operations, configure:
git config gc.auto 256git config gc.autopacklimit 50This triggers gc when object count exceeds configured thresholds.
What’s the difference between git gc and git maintenance?
Section titled “What’s the difference between git gc and git maintenance?”git gc focuses on pack file optimization, while git maintenance handles broader cleanup. Use both:
git gc && git maintenance run --allHow do I disable automatic gc?
Section titled “How do I disable automatic gc?”To disable automatic gc runs during porcelain commands, use:
git config gc.auto 0Applications of the git gc command
Section titled “Applications of the git gc command”- Optimizing repository performance and reducing storage
- Cleaning up after bulk imports or large object additions
- Periodic repository maintenance for long-term projects
- Troubleshooting strange repository behavior
- Preparing repositories for archive or backup
- Reducing repository size for distribution