Home·Terminal commands

git gc

git

Usage

git gc [--aggressive] [--prune=<date>]

Packs loose objects and drops unreachable ones to shrink .git; --prune=now also throws away the commits the reflog was holding, which removes your safety net for undoing a reset.

Common flags

FlagMeaning
--aggressiverepack harder; slow, occasionally worth it
--prune=<date>drop unreachable objects older than that
--prune=nowdrop them all, which also throws away reflog-only commits
--autodo nothing unless there is enough loose junk
--no-prunerepack but keep unreachable objects
-qno progress output

Examples

git gc

packs loose objects and shrinks .git

git gc --auto

the cheap check git itself runs after some commands

git gc --prune=now --aggressive

smallest result, and no way back to lost commits

The confusion is that undo has several shapes — moving where a branch points is not the same as adding a commit that reverses it.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does git gc do?

Packs loose objects and drops unreachable ones to shrink .git; --prune=now also throws away the commits the reflog was holding, which removes your safety net for undoing a reset.

Q. How do I type it?

git gc [--aggressive] [--prune=<date>] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

6 are listed here; the full set is in man git. This command sits under git.

Related commands

man page: man git-gc