- Developers who already know basic commit and branch actions
- Readers who want to understand command boundaries and risk
Command Reference
git-gc Tutorial
Explains how to use git-gc to run garbage collection and housekeeping.
- A basic mental model of worktree, index, and commits
- Comfort reading `git status` and a small commit graph
- Using local cleanup commands on already shared history
- Continuing to rewrite before confirming a recovery path
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 gcto 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 gcbefore archiving or backing up a repository to ensure the storage state is clean and efficient.
Diagram view
Special cases and boundaries
- Running
git gclocks the repository — no other Git operations can be executed during that time. - The
--pruneparameter controls how long unreachable objects are kept (default is 2 weeks); using--prune=nowremoves 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 runninggit gc. - Garbage collection can reorganize objects, packs, and reachability — confirm whether you still depend on temporarily unreachable objects before cleanup.