Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readGit

Schedule repo upkeep with git maintenance

Register a clone with git maintenance, run a named task such as prefetch by hand, and see what git maintenance start needs from the system.

If you run git gc by hand on a large clone, you can schedule smaller upkeep jobs with git maintenance. Register the repository, try a task, then start the schedule.

I ran everything below with HOME pointed at a scratch directory, so my real config stayed untouched.

Register the repo

git maintenance keeps its list in your global config. register adds the current repo to it.

$ git maintenance register
$ git config --global --list
maintenance.repo=/path/to/repo
$ tail -n 3 .git/config
[maintenance]
	auto = false
	strategy = incremental

Register changed two files. The global config lists the repo under maintenance.repo. Git sets maintenance.auto = false locally and selects the incremental strategy if you have no strategy configured.

Run a job by hand

Before you hand anything to a scheduler, run one job yourself. Name the task, because a bare git maintenance run started git gc in my sandbox when I watched it with GIT_TRACE=1.

--task=gc took my three-commit repo from 9 loose objects to 0 loose and 1 pack, which I read from git count-objects -v.

The task I would want on a big clone is prefetch. I made a second repo as origin on local disk, cloned it, and registered the clone.

Then I added a commit on main and a new topic branch in origin. Before the run, the clone had three refs: main, origin/HEAD and origin/main.

$ git maintenance run --task=prefetch
$ git for-each-ref --format='%(refname)'
refs/heads/main
refs/prefetch/remotes/origin/main
refs/prefetch/remotes/origin/topic
refs/remotes/origin/HEAD
refs/remotes/origin/main

The new commits arrived under refs/prefetch/, and origin/main stayed where it was. The objects are now local, and none of your branches or origin/* refs moved.

Start the schedule

register succeeded in a container with no scheduler, so it schedules nothing. git maintenance start is the command for the schedule, and it needs a scheduler to talk to.

$ git maintenance start
fatal: neither systemd timers nor crontab are available

My container has neither, so it stopped with exit code 128. Git uses launchd on macOS and Task Scheduler on Windows. The message names what the command looks for: systemd timers or crontab. On a machine with one of them, I would run start for the first clone, then register other clones to add them to the same schedule.

To take a repo off the list, run git maintenance unregister. In my sandbox it removed the maintenance.repo line from the global config, and git config --global --list printed nothing afterward.