6 git-maintenance - Run tasks to optimize Git repository data
12 'git maintenance' run [<options>]
17 Run tasks to optimize Git repository data, speeding up other Git commands
18 and reducing storage requirements for the repository.
20 Git commands that add repository data, such as `git add` or `git fetch`,
21 are optimized for a responsive user experience. These commands do not take
22 time to optimize the Git data, since such optimizations scale with the full
23 size of the repository while these user commands each perform a relatively
26 The `git maintenance` command provides flexibility for how to optimize the
33 Initialize Git config values so any scheduled maintenance will
34 start running on this repository. This adds the repository to the
35 `maintenance.repo` config variable in the current user's global
36 config and enables some recommended configuration values for
37 `maintenance.<task>.schedule`. The tasks that are enabled are safe
38 for running in the background without disrupting foreground
41 The `register` subcommand will also set the `maintenance.strategy` config
42 value to `incremental`, if this value is not previously set. The
43 `incremental` strategy uses the following schedule for each maintenance
48 * `commit-graph`: hourly.
50 * `loose-objects`: daily.
51 * `incremental-repack`: daily.
54 `git maintenance register` will also disable foreground maintenance by
55 setting `maintenance.auto = false` in the current repository. This config
56 setting will remain after a `git maintenance unregister` command.
59 Run one or more maintenance tasks. If one or more `--task` options
60 are specified, then those tasks are run in that order. Otherwise,
61 the tasks are determined by which `maintenance.<task>.enabled`
62 config options are true. By default, only `maintenance.gc.enabled`
66 Start running maintenance on the current repository. This performs
67 the same config updates as the `register` subcommand, then updates
68 the background scheduler to run `git maintenance run --scheduled`
72 Halt the background maintenance schedule. The current repository
73 is not removed from the list of maintained repositories, in case
74 the background maintenance is restarted later.
77 Remove the current repository from background maintenance. This
78 only removes the repository from the configured list. It does not
79 stop the background maintenance processes from running.
85 The `commit-graph` job updates the `commit-graph` files incrementally,
86 then verifies that the written data is correct. The incremental
87 write is safe to run alongside concurrent Git processes since it
88 will not expire `.graph` files that were in the previous
89 `commit-graph-chain` file. They will be deleted by a later run based
90 on the expiration delay.
93 The `prefetch` task updates the object directory with the latest
94 objects from all registered remotes. For each remote, a `git fetch`
95 command is run. The refmap is custom to avoid updating local or remote
96 branches (those in `refs/heads` or `refs/remotes`). Instead, the
97 remote refs are stored in `refs/prefetch/<remote>/`. Also, tags are
100 This is done to avoid disrupting the remote-tracking branches. The end users
101 expect these refs to stay unmoved unless they initiate a fetch. With prefetch
102 task, however, the objects necessary to complete a later real fetch would
103 already be obtained, so the real fetch would go faster. In the ideal case,
104 it will just become an update to a bunch of remote-tracking branches without
108 Clean up unnecessary files and optimize the local repository. "GC"
109 stands for "garbage collection," but this task performs many
110 smaller tasks. This task can be expensive for large repositories,
111 as it repacks all Git objects into a single pack-file. It can also
112 be disruptive in some situations, as it deletes stale data. See
113 linkgit:git-gc[1] for more details on garbage collection in Git.
116 The `loose-objects` job cleans up loose objects and places them into
117 pack-files. In order to prevent race conditions with concurrent Git
118 commands, it follows a two-step process. First, it deletes any loose
119 objects that already exist in a pack-file; concurrent Git processes
120 will examine the pack-file for the object data instead of the loose
121 object. Second, it creates a new pack-file (starting with "loose-")
122 containing a batch of loose objects. The batch size is limited to 50
123 thousand objects to prevent the job from taking too long on a
124 repository with many loose objects. The `gc` task writes unreachable
125 objects as loose objects to be cleaned up by a later step only if
126 they are not re-added to a pack-file; for this reason it is not
127 advisable to enable both the `loose-objects` and `gc` tasks at the
131 The `incremental-repack` job repacks the object directory
132 using the `multi-pack-index` feature. In order to prevent race
133 conditions with concurrent Git commands, it follows a two-step
134 process. First, it calls `git multi-pack-index expire` to delete
135 pack-files unreferenced by the `multi-pack-index` file. Second, it
136 calls `git multi-pack-index repack` to select several small
137 pack-files and repack them into a bigger one, and then update the
138 `multi-pack-index` entries that refer to the small pack-files to
139 refer to the new pack-file. This prepares those small pack-files
140 for deletion upon the next run of `git multi-pack-index expire`.
141 The selection of the small pack-files is such that the expected
142 size of the big pack-file is at least the batch size; see the
143 `--batch-size` option for the `repack` subcommand in
144 linkgit:git-multi-pack-index[1]. The default batch-size is zero,
145 which is a special case that attempts to repack all pack-files
146 into a single pack-file.
149 The `pack-refs` task collects the loose reference files and
150 collects them into a single file. This speeds up operations that
151 need to iterate across many references. See linkgit:git-pack-refs[1]
152 for more information.
157 When combined with the `run` subcommand, run maintenance tasks
158 only if certain thresholds are met. For example, the `gc` task
159 runs when the number of loose objects exceeds the number stored
160 in the `gc.auto` config setting, or when the number of pack-files
161 exceeds the `gc.autoPackLimit` config setting. Not compatible with
162 the `--schedule` option.
165 When combined with the `run` subcommand, run maintenance tasks
166 only if certain time conditions are met, as specified by the
167 `maintenance.<task>.schedule` config value for each `<task>`.
168 This config value specifies a number of seconds since the last
169 time that task ran, according to the `maintenance.<task>.lastRun`
170 config value. The tasks that are tested are those provided by
171 the `--task=<task>` option(s) or those with
172 `maintenance.<task>.enabled` set to true.
175 Do not report progress or other information over `stderr`.
178 If this option is specified one or more times, then only run the
179 specified tasks in the specified order. If no `--task=<task>`
180 arguments are specified, then only the tasks with
181 `maintenance.<task>.enabled` configured as `true` are considered.
182 See the 'TASKS' section for the list of accepted `<task>` values.
187 The `git maintenance` command is designed to simplify the repository
188 maintenance patterns while minimizing user wait time during Git commands.
189 A variety of configuration options are available to allow customizing this
190 process. The default maintenance options focus on operations that complete
191 quickly, even on large repositories.
193 Users may find some cases where scheduled maintenance tasks do not run as
194 frequently as intended. Each `git maintenance run` command takes a lock on
195 the repository's object database, and this prevents other concurrent
196 `git maintenance run` commands from running on the same repository. Without
197 this safeguard, competing processes could leave the repository in an
200 The background maintenance schedule runs `git maintenance run` processes
201 on an hourly basis. Each run executes the "hourly" tasks. At midnight,
202 that process also executes the "daily" tasks. At midnight on the first day
203 of the week, that process also executes the "weekly" tasks. A single
204 process iterates over each registered repository, performing the scheduled
205 tasks for that frequency. Depending on the number of registered
206 repositories and their sizes, this process may take longer than an hour.
207 In this case, multiple `git maintenance run` commands may run on the same
208 repository at the same time, colliding on the object database lock. This
209 results in one of the two tasks not running.
211 If you find that some maintenance windows are taking longer than one hour
212 to complete, then consider reducing the complexity of your maintenance
213 tasks. For example, the `gc` task is much slower than the
214 `incremental-repack` task. However, this comes at a cost of a slightly
215 larger object database. Consider moving more expensive tasks to be run
218 Expert users may consider scheduling their own maintenance tasks using a
219 different schedule than is available through `git maintenance start` and
220 Git configuration options. These users should be aware of the object
221 database lock and how concurrent `git maintenance run` commands behave.
222 Further, the `git gc` command should not be combined with
223 `git maintenance run` commands. `git gc` modifies the object database
224 but does not take the lock in the same way as `git maintenance run`. If
225 possible, use `git maintenance run --task=gc` instead of `git gc`.
227 The following sections describe the mechanisms put in place to run
228 background maintenance by `git maintenance start` and how to customize
231 BACKGROUND MAINTENANCE ON POSIX SYSTEMS
232 ---------------------------------------
234 The standard mechanism for scheduling background tasks on POSIX systems
235 is cron(8). This tool executes commands based on a given schedule. The
236 current list of user-scheduled tasks can be found by running `crontab -l`.
237 The schedule written by `git maintenance start` is similar to this:
239 -----------------------------------------------------------------------
240 # BEGIN GIT MAINTENANCE SCHEDULE
241 # The following schedule was created by Git
242 # Any edits made in this region might be
243 # replaced in the future by a Git command.
245 0 1-23 * * * "/<path>/git" --exec-path="/<path>" for-each-repo --config=maintenance.repo maintenance run --schedule=hourly
246 0 0 * * 1-6 "/<path>/git" --exec-path="/<path>" for-each-repo --config=maintenance.repo maintenance run --schedule=daily
247 0 0 * * 0 "/<path>/git" --exec-path="/<path>" for-each-repo --config=maintenance.repo maintenance run --schedule=weekly
249 # END GIT MAINTENANCE SCHEDULE
250 -----------------------------------------------------------------------
252 The comments are used as a region to mark the schedule as written by Git.
253 Any modifications within this region will be completely deleted by
254 `git maintenance stop` or overwritten by `git maintenance start`.
256 The `crontab` entry specifies the full path of the `git` executable to
257 ensure that the executed `git` command is the same one with which
258 `git maintenance start` was issued independent of `PATH`. If the same user
259 runs `git maintenance start` with multiple Git executables, then only the
260 latest executable is used.
262 These commands use `git for-each-repo --config=maintenance.repo` to run
263 `git maintenance run --schedule=<frequency>` on each repository listed in
264 the multi-valued `maintenance.repo` config option. These are typically
265 loaded from the user-specific global config. The `git maintenance` process
266 then determines which maintenance tasks are configured to run on each
267 repository with each `<frequency>` using the `maintenance.<task>.schedule`
268 config options. These values are loaded from the global or repository
271 If the config values are insufficient to achieve your desired background
272 maintenance schedule, then you can create your own schedule. If you run
273 `crontab -e`, then an editor will load with your user-specific `cron`
274 schedule. In that editor, you can add your own schedule lines. You could
275 start by adapting the default schedule listed earlier, or you could read
276 the crontab(5) documentation for advanced scheduling techniques. Please
277 do use the full path and `--exec-path` techniques from the default
278 schedule to ensure you are executing the correct binaries in your
282 BACKGROUND MAINTENANCE ON MACOS SYSTEMS
283 ---------------------------------------
285 While macOS technically supports `cron`, using `crontab -e` requires
286 elevated privileges and the executed process does not have a full user
287 context. Without a full user context, Git and its credential helpers
288 cannot access stored credentials, so some maintenance tasks are not
291 Instead, `git maintenance start` interacts with the `launchctl` tool,
292 which is the recommended way to schedule timed jobs in macOS. Scheduling
293 maintenance through `git maintenance (start|stop)` requires some
294 `launchctl` features available only in macOS 10.11 or later.
296 Your user-specific scheduled tasks are stored as XML-formatted `.plist`
297 files in `~/Library/LaunchAgents/`. You can see the currently-registered
298 tasks using the following command:
300 -----------------------------------------------------------------------
301 $ ls ~/Library/LaunchAgents/org.git-scm.git*
302 org.git-scm.git.daily.plist
303 org.git-scm.git.hourly.plist
304 org.git-scm.git.weekly.plist
305 -----------------------------------------------------------------------
307 One task is registered for each `--schedule=<frequency>` option. To
308 inspect how the XML format describes each schedule, open one of these
309 `.plist` files in an editor and inspect the `<array>` element following
310 the `<key>StartCalendarInterval</key>` element.
312 `git maintenance start` will overwrite these files and register the
313 tasks again with `launchctl`, so any customizations should be done by
314 creating your own `.plist` files with distinct names. Similarly, the
315 `git maintenance stop` command will unregister the tasks with `launchctl`
316 and delete the `.plist` files.
318 To create more advanced customizations to your background tasks, see
319 launchctl.plist(5) for more information.
322 BACKGROUND MAINTENANCE ON WINDOWS SYSTEMS
323 -----------------------------------------
325 Windows does not support `cron` and instead has its own system for
326 scheduling background tasks. The `git maintenance start` command uses
327 the `schtasks` command to submit tasks to this system. You can inspect
328 all background tasks using the Task Scheduler application. The tasks
329 added by Git have names of the form `Git Maintenance (<frequency>)`.
330 The Task Scheduler GUI has ways to inspect these tasks, but you can also
331 export the tasks to XML files and view the details there.
333 Note that since Git is a console application, these background tasks
334 create a console window visible to the current user. This can be changed
335 manually by selecting the "Run whether user is logged in or not" option
336 in Task Scheduler. This change requires a password input, which is why
337 `git maintenance start` does not select it by default.
339 If you want to customize the background tasks, please rename the tasks
340 so future calls to `git maintenance (start|stop)` do not overwrite your
346 Part of the linkgit:git[1] suite