git gc Command: Repack Repos in CI
git gc packs loose objects and prunes unreachable ones to reduce repo size and speed up operations.
On runners that cache a clone across many builds, the .git directory accumulates loose objects. Running gc periodically keeps the cached repo lean and fast to fetch.
Common flags
--auto- run gc only if the repo has crossed internal thresholds (cheap to call)--prune=<date>- prune unreachable objects older than the given date (e.g. now)--aggressive- repack more thoroughly at higher CPU cost--no-cruft- write pruned objects loose instead of in a cruft pack--quiet/-q- suppress progress output
Example
shell
# Keep a cached CI checkout compact between builds
git gc --auto --prune=nowIn CI
Use git gc --auto in cache-restore steps: it is nearly free when nothing is needed and only repacks when the repo has grown. Avoid --aggressive in CI unless disk size is critical, since it is CPU-heavy. Ephemeral runners rarely need gc at all.
Key takeaways
- git gc --auto is a cheap no-op unless the repo actually needs repacking.
- --prune=now reclaims space from unreachable objects in cached checkouts.
- Skip --aggressive in CI; its CPU cost rarely pays off on disposable runners.
Related guides
git clean Command: Remove Untracked Files in CIgit clean deletes untracked files. Reference for -f, -d, and -x (git clean -fdx) to fully reset a working tre…
git fetch Command: Update Refs in CIgit fetch downloads objects and refs from a remote without merging. Reference for --depth, --unshallow, --tag…
git archive Command: Export Trees in CIgit archive exports a tree as a tar or zip. Reference for --format, --prefix, and pathspecs to build clean so…
git clone Command: Clone Repos in CIgit clone copies a remote repository into a new directory. Reference for --depth, --branch, --filter, and --r…