LinuxCommandLibrary

git-gc

Optimize Git repository by garbage collecting

TLDR

Optimise the repository

$ git gc
copy

Aggressively optimise, takes more time
$ git gc --aggressive
copy

Do not prune loose objects (prunes by default)
$ git gc --no-prune
copy

Suppress all output
$ git gc --quiet
copy

Display help
$ git gc --help
copy

SYNOPSIS

git gc [--aggressive] [--auto] [--force] [--keep-largest-empty-commit] [--no-prune] [--prune=<date>] [--quiet] [-c <name>=<value>]

PARAMETERS

--aggressive
    Perform more thorough garbage collection with better compression (slower)

--auto
    Run only if repository meets auto-gc thresholds (default behavior)

--force
    Force garbage collection even if auto conditions not met

--keep-largest-empty-commit
    Protect largest empty commit from pruning

--no-prune
    Do not prune loose objects

--prune=<date>
    Prune loose objects older than <date> (default: 2 weeks ago)

--quiet
    Suppress all progress reporting

-c <name>=<value>
    Override git-config value for this invocation

DESCRIPTION

The git gc command performs garbage collection on a Git repository, reclaiming space and improving performance. It packs loose objects into efficient packfiles using git repack, removes unreachable objects, prunes reflogs based on expiration settings, and compresses objects further.

By default, it prunes loose objects older than 2 weeks and packs objects aggressively if configured. Running git gc manually is rarely needed, as Git invokes it automatically during operations like receiving pushes (via receive.autogc) or when thresholds are met (via gc.auto, e.g., 6700 loose objects or packfiles exceeding 50 MiB).

This maintenance reduces disk usage, speeds up operations like git fetch and git checkout, and keeps the repository healthy. Use --aggressive for deeper cleanup on idle repos, but it takes longer. Always safe to run, though it locks the repository briefly.

CAVEATS

Can be resource-intensive on large repositories; --aggressive takes significantly longer. Locks the repository during execution. Avoid in automated scripts without --quiet. Does not remove repository entirely.

KEY CONFIGURATIONS

Tune with gc.auto (threshold for auto-run, default 6700), gc.reflogExpire (unreachable reflog expiry, default 90 days), gc.bigPackThreshold (avoid repacking large packs).

WHEN TO RUN

Manually after heavy usage or before archiving. Git runs automatically post-fetch/push if receive.autogc=1.

HISTORY

Introduced in Git 1.5.0 (2007) as core maintenance tool. Evolved with auto-gc in 1.7.1 (2010) for automatic invocation. Ongoing refinements for split-index support and empty commit handling in later versions.

SEE ALSO

Copied to clipboard