Git Maintenance and Repository Housekeeping: gc, pack-refs, and commit-graph

git maintenance: keep a large WordPress repository fast

A repository that has been worked on for years collects loose objects, thousands of refs from old branches and pull requests, and pack files that were never combined. git status, git log and fetches get slower, and nobody notices when it happened. Git has a built-in scheduler for housekeeping, git maintenance, which runs the right tasks in the background instead of a big git gc at random moments.

Problem: A site repository with eight years of history and 3,000 remote branches takes four seconds for git status and twenty for git log --graph on a developer laptop, and every few days a fetch stops for a minute with “Auto packing the repository”.

Solution: Prune stale remote branches, enable git maintenance start so that prefetch, commit-graph, loose-object packing and incremental repack run hourly and daily in the background, turn on the commit-graph and the file system monitor, and run a full git gc only once after cleaning up.

Clean up once

git fetch --prune                       # remove remote branches that no longer exist
git branch --merged main | grep -v ' main$' | xargs -r git branch -d   # local branches already merged
git count-objects -vH                   # size and number of loose objects before
git gc --prune=now
git count-objects -vH                   # after

--prune on fetch removes origin/* refs whose branches were deleted on the server; set git config --global fetch.prune true to make it permanent. git gc packs loose objects, removes unreachable ones and packs refs into packed-refs, which makes ref lookups fast with thousands of branches.

Background maintenance

git maintenance start
git config --get-all maintenance.repo

git maintenance start registers the repository and creates scheduled jobs for the user (launchd on macOS, systemd timers or cron on Linux, Task Scheduler on Windows). The default schedule:

  • hourly: prefetch (fetch in the background into a hidden ref namespace, so the next real git fetch has little to download) and commit-graph (keeps the commit-graph file up to date).
  • daily: loose-objects (packs loose objects) and incremental-repack (combines small pack files without rewriting everything).

It also sets maintenance.auto = false for the repository, so the blocking “Auto packing” during normal commands no longer happens. git maintenance run --task=gc runs a single task by hand; git maintenance stop removes the schedule.

Faster everyday commands

git config core.commitGraph true
git config fetch.writeCommitGraph true
git config core.fsmonitor true          # Git 2.37+: built-in file system monitor on macOS and Windows
git config core.untrackedCache true

The commit-graph is a precomputed index of commits and their parents, which makes git log --graph, merge-base and reachability checks much faster in long histories. The file system monitor watches the working tree, so git status does not have to scan every file of the repository; it matters for WordPress repositories with large vendor or node_modules folders that are not ignored. On Linux, the built-in monitor is not available; Watchman can be connected with a hook instead.

When the size comes from files

If git count-objects -vH still shows gigabytes, the history contains large files: database dumps, ZIPs, media. Maintenance cannot shrink those. Find them with git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | sort -k3 -n | tail -20, remove them from history with git filter-repo, and track new binary files with Git LFS. For working with only part of a big repository: partial clones and sparse checkout.

NOTE: All of this is local: maintenance on one clone does not change the remote or other developers’ clones. On the server side, GitHub and GitLab run their own maintenance. A server that hosts bare repositories (a deploy target) can use git maintenance in the same way under the deploy user.