All commands

git gc

intermediate

Sweep unreachable objects and compress your repository.

What it does

git gc is Git's garbage collector. It deletes objects with no refs pointing to them (once they're old enough), repacks loose objects into efficient packfiles, and prunes the stale reflog. It shrinks .git and speeds up every subsequent operation.

commit74d3b9f0…
treef2b0c9d1a4e8…
parent9a2f1c0b7d3e…
authorAda <ada@dev> 12:07 +0200
committerAda <ada@dev> 12:07 +0200
messagefeat: add dark mode
treef2b0c9d1…
blob aa8f1e… README.md
blob 3c2d09… src/main.ts
tree b7e1a2… src/
blobaa8f1e…
header: blob + size
content bytes…
SHA-1 → unique key of content

git gc collapses loose objects into one packed, delta-compressed packfile

Watch it — press play

A narrated, step-by-step walkthrough that shows how to do it — not just theory. It adapts to the learner mode you picked in the top bar.

1 / 2

Step 1

The repository gets fat

Every add and commit writes loose, individually-compressed files. Thousands later, .git bloats and slows.

git gc --aggressive
your-project/ · after git init
.git
├── HEAD
├── index
├── objects/
│ ├── info/
│ └── pack/
└── refs/
├── heads/
└── tags/

Loose objects pile up as you work

Under the hood

1

Reachability rules

An object is alive if it can be reached from a ref: branches, tags, the index, or the reflog. Anything else is a candidate for pruning — but only if it's aged past gc.reflogExpire (default 90 days).

objects ──reachable via──> refs/heads/*, refs/tags/*,
  reflog, index, HEAD
  ↓ unreachable & old
  pruned by git gc
2

Loose objects → packfiles

Loose objects are compressed individually (cheap to write, slow to read). git gc repacks them into a single packfile with delta compression — storing only the differences between similar blobs. Massive space savings.

3

It runs automatically

Git auto-runs a light gc whenever it senses too many loose objects (about 7,000). You rarely need to invoke it by hand — but --aggressive helps a bloated repo.

Aliases the pros type

Memorize the git gc concept, then let one of these shortcuts make it instant. Aliases are real git config keys — add one with:

git config --global alias.st # → git gc --aggressive
AliasWhy people use it
g gca

The full clean, when .git has become bloated.

g gc --prune=now

Force-remove recoverable-but-really-dead commits.

Command anatomy

git gc [--aggressive]
--aggressive

More thorough — find better deltas at the cost of time.

--prune=<date>

Prune unreachable objects older than a date or in 'now'.

--auto

Run the automatic routine Git uses internally.

When to reach for it

A giant monorepo clone hanging → git gc --aggressive after fixing .gitignore.

You force-pushed away a big binary at a random commit → gc finally removes it from .git.

Sending a repo by zip: pack everything first with gc so the bundle is small.

Pro tip

Before gc, check what's truly dead: `git fsck --lost-found` lists dangling commits (unreachable but not yet pruned). You can watch a commit you 'deleted' linger there peacefully until gc reaps it.

Go deeper with