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

Large WordPress project repositories accumulate loose objects, packed-ref files, and stale remote tracking branches over time. Git’s built-in git maintenance command (available since Git 2.30) automates the housekeeping tasks that used to require manually running git gc, git pack-refs, and git commit-graph write — and it can be scheduled to run in the background on a daily cron without blocking your work.

Problem: A WordPress plugin's Git repository accumulates loose objects, redundant pack files, and dangling references over time — git log and git status slow down, and the repository size grows unnecessarily.

Solution: Run git maintenance start to enable background maintenance tasks: it schedules gc, pack-refs, commit-graph, and prefetch automatically. For one-off cleanup, run git gc --aggressive to repack all objects into a single pack file and prune unreachable objects older than two weeks.


The commands below set up background maintenance on a repo, explain each maintenance task, show how to inspect the commit-graph for performance gains, and clean up stale remote branches safely.


# 1. Enable background maintenance (registers OS-level scheduler)
git maintenance start
# On macOS: registers a launchd plist; on Linux: a systemd user timer

# 2. Run all maintenance tasks immediately (useful after a large import)
git maintenance run --auto

# 3. Run specific tasks manually
git maintenance run --task=gc              # pack loose objects, prune reflog
git maintenance run --task=pack-refs      # consolidate loose refs into packed-refs
git maintenance run --task=commit-graph   # write/update .git/objects/info/commit-graph
git maintenance run --task=prefetch       # background-fetch all remotes
git maintenance run --task=loose-objects  # pack loose objects (lighter than gc)
git maintenance run --task=incremental-repack  # repack without full gc overhead

# 4. Inspect commit-graph (speeds up git log, blame, and branch operations)
git commit-graph verify           # verify integrity
git log --use-commit-graph --oneline -10   # force use of commit-graph

# 5. Prune stale remote-tracking branches
git fetch --prune origin          # remove refs deleted on remote
git remote prune origin           # same, without fetching

# 6. Clean up merged local branches
git branch --merged main | grep -v '^\*\|main\|develop' | xargs git branch -d

# 7. Check repo health and object count before/after
git count-objects -vH             # human-readable object stats
git fsck --unreachable --no-reflogs  # find dangling objects


# 8. Configure maintenance schedule (hourly prefetch, daily gc)
git config maintenance.prefetch.schedule hourly
git config maintenance.gc.schedule daily
git config maintenance.commit-graph.schedule hourly
git config maintenance.loose-objects.schedule daily
git config maintenance.incremental-repack.schedule daily

# 9. Verify scheduled tasks
git maintenance list-tasks        # show configured tasks and schedules

# 10. Stop background maintenance (removes OS scheduler entries)
git maintenance stop


NOTE: git maintenance start schedules tasks using your OS scheduler (launchd on macOS, systemd on Linux) under your user account — it does not require root and will not affect other users; the tasks run only when the machine is idle and are safe to leave enabled permanently on developer workstations.