Command Reference

git-gc Tutorial

Explains how to use git-gc to run garbage collection and housekeeping.

Who This Is For
  • Developers who already know basic commit and branch actions
  • Readers who want to understand command boundaries and risk
Prerequisites
  • A basic mental model of worktree, index, and commits
  • Comfort reading `git status` and a small commit graph
Common Risks
  • Using local cleanup commands on already shared history
  • Continuing to rewrite before confirming a recovery path

Citations & Further Reading

  1. git-scm.com — Git gc [Official]

What you will learn

  • Understand the core purpose of git-gc Tutorial
  • Master the basic usage and common options of git-gc Tutorial
  • Explains how to use git-gc to run garbage collection and housekeeping.
  • Understand key concepts: When it is a good fit
  • Know when to use this feature and when to avoid it

Start with a problem

You're working in a Git repository and need to perform a specific task — but you're not sure which command or option is the right fit, or what this command can and cannot do.

The short version

git-gc is used to run garbage collection and housekeeping.

When it is a good fit

  • when you need to run garbage collection and housekeeping
  • when you want this step to be repeatable instead of ad hoc
  • when you need a clearer mental model of what Git is recording or updating

Basic example

git gc

What to watch most closely

Advanced commands are not always part of the daily path, but mistakes with them usually cost more to recover from.

A safer working habit

Rehearse the flow on a small reproducible history before running it in a production repository.

Useful angles for understanding it

  • Handle more complex collaboration or history-analysis tasks
  • Turn one-off steps into repeatable routines
  • Reduce the risk of advanced operations

Related reading

Read it alongside git status, git log, and git show so it is easier to see how the command changes history, refs, the index, or the working tree.

What problem this command solves in a workflow

git gc focuses on the actual maintenance and optimization of repository object storage. It compresses loose objects into pack files, cleans up expired reflog entries, and removes unreachable objects. This is an operational command that actually changes repository content.

Typical use cases

  • Run git gc to compress and organize loose objects when the repository has grown large due to too many loose objects.
  • Clean up unreachable objects after running many rebases, resets, or amends by using git gc --prune=now.
  • Run git gc before archiving or backing up a repository to ensure the storage state is clean and efficient.

Diagram view

Object storage and maintenance viewStorage-oriented commands work against the object database, pack files, and reachability model to verify, analyze, or clean repository state.
Storage objects
Object databasePack filesReachability information
Results
Compressed pack filesCleaned object databaseOptimized storage space
This is an operational command that modifies the repository. Confirm no other Git operations are in progress before running it.

Special cases and boundaries

  • Running git gc locks the repository — no other Git operations can be executed during that time.
  • The --prune parameter controls how long unreachable objects are kept (default is 2 weeks); using --prune=now removes them immediately, so use with caution.
  • If you are worried about accidentally deleting objects, first check the state with git fsck, or back up the repository before running git gc.
  • Garbage collection can reorganize objects, packs, and reachability — confirm whether you still depend on temporarily unreachable objects before cleanup.

Try it yourself

  1. Practice the git-gc command in a test repository and observe state changes before and after
  2. Experiment with different options and compare the output differences
  3. Simulate a real scenario where you would need to use this, and walk through the full process

Further reading

Keep going on the same topic: