Skip to content

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.

Terminal window
git gc [--aggressive] [--auto] [--[no-]detach] [--quiet]
[--prune=<date> | --no-prune] [--force] [--keep-largest-pack]
OptionDescription
—aggressiveAggressively optimize (much slower)
—autoCheck if housekeeping needed, exit if not
—detachRun in background (default)
—no-detachRun in foreground
—quietSuppress progress reports
—prune=Prune loose objects older than date (default: 2 weeks)
—no-pruneDon’t prune loose objects
—forceForce run even if another gc is running
—keep-largest-packConsolidate all except largest pack
ParameterDescription
(none)Operates on current repository
Terminal window
git gc

Run standard garbage collection and optimization.

Terminal window
git gc --aggressive

Spend extra time for maximum optimization (not usually worth it).

Terminal window
git gc --auto

Only run if repository needs housekeeping.

Terminal window
git gc --prune=now

Prune loose objects regardless of age (risky if repo is active).

Terminal window
git gc --force

Run garbage collection despite concurrent gc processes.

Terminal window
git gc --quiet

Run without progress output.

Terminal window
git gc --prune=1.month.ago

Prune objects older than 1 month.

Terminal window
git gc --keep-largest-pack

Preserve largest pack, consolidate others.

Terminal window
git gc --no-detach

Run in foreground instead of background.

Terminal window
git gc && git maintenance start

Run 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:

Terminal window
git gc

This compresses objects, removes unreachable ones, and optimizes the repository.

To check if maintenance is needed without forcing it, use:

Terminal window
git gc --auto

This exits early if no housekeeping is required based on configuration thresholds.

To use aggressive optimization when optimization is critical, run:

Terminal window
git gc --aggressive

This 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:

Terminal window
git gc --prune=1.week.ago

This prunes objects older than the specified time.

To run gc in the background (default), use:

Terminal window
git gc

Or explicitly:

Terminal window
git gc --detach

To run gc in the foreground, use:

Terminal window
git gc --no-detach

This is useful for scripts that need gc to complete before continuing.

To force gc even if another instance is running, use:

Terminal window
git gc --force

This 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:

Terminal window
git config gc.auto 256
git config gc.autopacklimit 50

This 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:

Terminal window
git gc && git maintenance run --all

To disable automatic gc runs during porcelain commands, use:

Terminal window
git config gc.auto 0
  1. Optimizing repository performance and reducing storage
  2. Cleaning up after bulk imports or large object additions
  3. Periodic repository maintenance for long-term projects
  4. Troubleshooting strange repository behavior
  5. Preparing repositories for archive or backup
  6. Reducing repository size for distribution