another batch after 2.47-rc0
[git.git] / Documentation / git-maintenance.txt
blob9d968191331080c8e14557e08a500297672660ce
1 git-maintenance(1)
2 ==================
4 NAME
5 ----
6 git-maintenance - Run tasks to optimize Git repository data
9 SYNOPSIS
10 --------
11 [verse]
12 'git maintenance' run [<options>]
13 'git maintenance' start [--scheduler=<scheduler>]
14 'git maintenance' (stop|register|unregister) [<options>]
17 DESCRIPTION
18 -----------
19 Run tasks to optimize Git repository data, speeding up other Git commands
20 and reducing storage requirements for the repository.
22 Git commands that add repository data, such as `git add` or `git fetch`,
23 are optimized for a responsive user experience. These commands do not take
24 time to optimize the Git data, since such optimizations scale with the full
25 size of the repository while these user commands each perform a relatively
26 small action.
28 The `git maintenance` command provides flexibility for how to optimize the
29 Git repository.
31 SUBCOMMANDS
32 -----------
34 run::
35         Run one or more maintenance tasks. If one or more `--task` options
36         are specified, then those tasks are run in that order. Otherwise,
37         the tasks are determined by which `maintenance.<task>.enabled`
38         config options are true. By default, only `maintenance.gc.enabled`
39         is true.
41 start::
42         Start running maintenance on the current repository. This performs
43         the same config updates as the `register` subcommand, then updates
44         the background scheduler to run `git maintenance run --scheduled`
45         on an hourly basis.
47 stop::
48         Halt the background maintenance schedule. The current repository
49         is not removed from the list of maintained repositories, in case
50         the background maintenance is restarted later.
52 register::
53         Initialize Git config values so any scheduled maintenance will start
54         running on this repository. This adds the repository to the
55         `maintenance.repo` config variable in the current user's global config,
56         or the config specified by --config-file option, and enables some
57         recommended configuration values for `maintenance.<task>.schedule`. The
58         tasks that are enabled are safe for running in the background without
59         disrupting foreground processes.
61 The `register` subcommand will also set the `maintenance.strategy` config
62 value to `incremental`, if this value is not previously set. The
63 `incremental` strategy uses the following schedule for each maintenance
64 task:
67 * `gc`: disabled.
68 * `commit-graph`: hourly.
69 * `prefetch`: hourly.
70 * `loose-objects`: daily.
71 * `incremental-repack`: daily.
74 `git maintenance register` will also disable foreground maintenance by
75 setting `maintenance.auto = false` in the current repository. This config
76 setting will remain after a `git maintenance unregister` command.
78 unregister::
79         Remove the current repository from background maintenance. This
80         only removes the repository from the configured list. It does not
81         stop the background maintenance processes from running.
83 The `unregister` subcommand will report an error if the current repository
84 is not already registered. Use the `--force` option to return success even
85 when the current repository is not registered.
87 TASKS
88 -----
90 commit-graph::
91         The `commit-graph` job updates the `commit-graph` files incrementally,
92         then verifies that the written data is correct. The incremental
93         write is safe to run alongside concurrent Git processes since it
94         will not expire `.graph` files that were in the previous
95         `commit-graph-chain` file. They will be deleted by a later run based
96         on the expiration delay.
98 prefetch::
99         The `prefetch` task updates the object directory with the latest
100         objects from all registered remotes. For each remote, a `git fetch`
101         command is run. The configured refspec is modified to place all
102         requested refs within `refs/prefetch/`. Also, tags are not updated.
104 This is done to avoid disrupting the remote-tracking branches. The end users
105 expect these refs to stay unmoved unless they initiate a fetch.  However,
106 with the prefetch task, the objects necessary to complete a later real fetch
107 would already be obtained, making the real fetch faster.  In the ideal case,
108 it will just become an update to a bunch of remote-tracking branches without
109 any object transfer.
111 The `remote.<name>.skipFetchAll` configuration can be used to
112 exclude a particular remote from getting prefetched.
114 gc::
115         Clean up unnecessary files and optimize the local repository. "GC"
116         stands for "garbage collection," but this task performs many
117         smaller tasks. This task can be expensive for large repositories,
118         as it repacks all Git objects into a single pack-file. It can also
119         be disruptive in some situations, as it deletes stale data. See
120         linkgit:git-gc[1] for more details on garbage collection in Git.
122 loose-objects::
123         The `loose-objects` job cleans up loose objects and places them into
124         pack-files. In order to prevent race conditions with concurrent Git
125         commands, it follows a two-step process. First, it deletes any loose
126         objects that already exist in a pack-file; concurrent Git processes
127         will examine the pack-file for the object data instead of the loose
128         object. Second, it creates a new pack-file (starting with "loose-")
129         containing a batch of loose objects. The batch size is limited to 50
130         thousand objects to prevent the job from taking too long on a
131         repository with many loose objects. The `gc` task writes unreachable
132         objects as loose objects to be cleaned up by a later step only if
133         they are not re-added to a pack-file; for this reason it is not
134         advisable to enable both the `loose-objects` and `gc` tasks at the
135         same time.
137 incremental-repack::
138         The `incremental-repack` job repacks the object directory
139         using the `multi-pack-index` feature. In order to prevent race
140         conditions with concurrent Git commands, it follows a two-step
141         process. First, it calls `git multi-pack-index expire` to delete
142         pack-files unreferenced by the `multi-pack-index` file. Second, it
143         calls `git multi-pack-index repack` to select several small
144         pack-files and repack them into a bigger one, and then update the
145         `multi-pack-index` entries that refer to the small pack-files to
146         refer to the new pack-file. This prepares those small pack-files
147         for deletion upon the next run of `git multi-pack-index expire`.
148         The selection of the small pack-files is such that the expected
149         size of the big pack-file is at least the batch size; see the
150         `--batch-size` option for the `repack` subcommand in
151         linkgit:git-multi-pack-index[1]. The default batch-size is zero,
152         which is a special case that attempts to repack all pack-files
153         into a single pack-file.
155 pack-refs::
156         The `pack-refs` task collects the loose reference files and
157         collects them into a single file. This speeds up operations that
158         need to iterate across many references. See linkgit:git-pack-refs[1]
159         for more information.
161 OPTIONS
162 -------
163 --auto::
164         When combined with the `run` subcommand, run maintenance tasks
165         only if certain thresholds are met. For example, the `gc` task
166         runs when the number of loose objects exceeds the number stored
167         in the `gc.auto` config setting, or when the number of pack-files
168         exceeds the `gc.autoPackLimit` config setting. Not compatible with
169         the `--schedule` option.
171 --schedule::
172         When combined with the `run` subcommand, run maintenance tasks
173         only if certain time conditions are met, as specified by the
174         `maintenance.<task>.schedule` config value for each `<task>`.
175         This config value specifies a number of seconds since the last
176         time that task ran, according to the `maintenance.<task>.lastRun`
177         config value. The tasks that are tested are those provided by
178         the `--task=<task>` option(s) or those with
179         `maintenance.<task>.enabled` set to true.
181 --quiet::
182         Do not report progress or other information over `stderr`.
184 --task=<task>::
185         If this option is specified one or more times, then only run the
186         specified tasks in the specified order. If no `--task=<task>`
187         arguments are specified, then only the tasks with
188         `maintenance.<task>.enabled` configured as `true` are considered.
189         See the 'TASKS' section for the list of accepted `<task>` values.
191 --scheduler=auto|crontab|systemd-timer|launchctl|schtasks::
192         When combined with the `start` subcommand, specify the scheduler
193         for running the hourly, daily and weekly executions of
194         `git maintenance run`.
195         Possible values for `<scheduler>` are `auto`, `crontab`
196         (POSIX), `systemd-timer` (Linux), `launchctl` (macOS), and
197         `schtasks` (Windows). When `auto` is specified, the
198         appropriate platform-specific scheduler is used; on Linux,
199         `systemd-timer` is used if available, otherwise
200         `crontab`. Default is `auto`.
203 TROUBLESHOOTING
204 ---------------
205 The `git maintenance` command is designed to simplify the repository
206 maintenance patterns while minimizing user wait time during Git commands.
207 A variety of configuration options are available to allow customizing this
208 process. The default maintenance options focus on operations that complete
209 quickly, even on large repositories.
211 Users may find some cases where scheduled maintenance tasks do not run as
212 frequently as intended. Each `git maintenance run` command takes a lock on
213 the repository's object database, and this prevents other concurrent
214 `git maintenance run` commands from running on the same repository. Without
215 this safeguard, competing processes could leave the repository in an
216 unpredictable state.
218 The background maintenance schedule runs `git maintenance run` processes
219 on an hourly basis. Each run executes the "hourly" tasks. At midnight,
220 that process also executes the "daily" tasks. At midnight on the first day
221 of the week, that process also executes the "weekly" tasks. A single
222 process iterates over each registered repository, performing the scheduled
223 tasks for that frequency. Depending on the number of registered
224 repositories and their sizes, this process may take longer than an hour.
225 In this case, multiple `git maintenance run` commands may run on the same
226 repository at the same time, colliding on the object database lock. This
227 results in one of the two tasks not running.
229 If you find that some maintenance windows are taking longer than one hour
230 to complete, then consider reducing the complexity of your maintenance
231 tasks. For example, the `gc` task is much slower than the
232 `incremental-repack` task. However, this comes at a cost of a slightly
233 larger object database. Consider moving more expensive tasks to be run
234 less frequently.
236 Expert users may consider scheduling their own maintenance tasks using a
237 different schedule than is available through `git maintenance start` and
238 Git configuration options. These users should be aware of the object
239 database lock and how concurrent `git maintenance run` commands behave.
240 Further, the `git gc` command should not be combined with
241 `git maintenance run` commands. `git gc` modifies the object database
242 but does not take the lock in the same way as `git maintenance run`. If
243 possible, use `git maintenance run --task=gc` instead of `git gc`.
245 The following sections describe the mechanisms put in place to run
246 background maintenance by `git maintenance start` and how to customize
247 them.
249 BACKGROUND MAINTENANCE ON POSIX SYSTEMS
250 ---------------------------------------
252 The standard mechanism for scheduling background tasks on POSIX systems
253 is cron(8). This tool executes commands based on a given schedule. The
254 current list of user-scheduled tasks can be found by running `crontab -l`.
255 The schedule written by `git maintenance start` is similar to this:
257 -----------------------------------------------------------------------
258 # BEGIN GIT MAINTENANCE SCHEDULE
259 # The following schedule was created by Git
260 # Any edits made in this region might be
261 # replaced in the future by a Git command.
263 0 1-23 * * * "/<path>/git" --exec-path="/<path>" for-each-repo --config=maintenance.repo maintenance run --schedule=hourly
264 0 0 * * 1-6 "/<path>/git" --exec-path="/<path>" for-each-repo --config=maintenance.repo maintenance run --schedule=daily
265 0 0 * * 0 "/<path>/git" --exec-path="/<path>" for-each-repo --config=maintenance.repo maintenance run --schedule=weekly
267 # END GIT MAINTENANCE SCHEDULE
268 -----------------------------------------------------------------------
270 The comments are used as a region to mark the schedule as written by Git.
271 Any modifications within this region will be completely deleted by
272 `git maintenance stop` or overwritten by `git maintenance start`.
274 The `crontab` entry specifies the full path of the `git` executable to
275 ensure that the executed `git` command is the same one with which
276 `git maintenance start` was issued independent of `PATH`. If the same user
277 runs `git maintenance start` with multiple Git executables, then only the
278 latest executable is used.
280 These commands use `git for-each-repo --config=maintenance.repo` to run
281 `git maintenance run --schedule=<frequency>` on each repository listed in
282 the multi-valued `maintenance.repo` config option. These are typically
283 loaded from the user-specific global config. The `git maintenance` process
284 then determines which maintenance tasks are configured to run on each
285 repository with each `<frequency>` using the `maintenance.<task>.schedule`
286 config options. These values are loaded from the global or repository
287 config values.
289 If the config values are insufficient to achieve your desired background
290 maintenance schedule, then you can create your own schedule. If you run
291 `crontab -e`, then an editor will load with your user-specific `cron`
292 schedule. In that editor, you can add your own schedule lines. You could
293 start by adapting the default schedule listed earlier, or you could read
294 the crontab(5) documentation for advanced scheduling techniques. Please
295 do use the full path and `--exec-path` techniques from the default
296 schedule to ensure you are executing the correct binaries in your
297 schedule.
300 BACKGROUND MAINTENANCE ON LINUX SYSTEMD SYSTEMS
301 -----------------------------------------------
303 While Linux supports `cron`, depending on the distribution, `cron` may
304 be an optional package not necessarily installed. On modern Linux
305 distributions, systemd timers are superseding it.
307 If user systemd timers are available, they will be used as a replacement
308 of `cron`.
310 In this case, `git maintenance start` will create user systemd timer units
311 and start the timers. The current list of user-scheduled tasks can be found
312 by running `systemctl --user list-timers`. The timers written by `git
313 maintenance start` are similar to this:
315 -----------------------------------------------------------------------
316 $ systemctl --user list-timers
317 NEXT                         LEFT          LAST                         PASSED     UNIT                         ACTIVATES
318 Thu 2021-04-29 19:00:00 CEST 42min left    Thu 2021-04-29 18:00:11 CEST 17min ago  git-maintenance@hourly.timer git-maintenance@hourly.service
319 Fri 2021-04-30 00:00:00 CEST 5h 42min left Thu 2021-04-29 00:00:11 CEST 18h ago    git-maintenance@daily.timer  git-maintenance@daily.service
320 Mon 2021-05-03 00:00:00 CEST 3 days left   Mon 2021-04-26 00:00:11 CEST 3 days ago git-maintenance@weekly.timer git-maintenance@weekly.service
321 -----------------------------------------------------------------------
323 One timer is registered for each `--schedule=<frequency>` option.
325 The definition of the systemd units can be inspected in the following files:
327 -----------------------------------------------------------------------
328 ~/.config/systemd/user/git-maintenance@.timer
329 ~/.config/systemd/user/git-maintenance@.service
330 ~/.config/systemd/user/timers.target.wants/git-maintenance@hourly.timer
331 ~/.config/systemd/user/timers.target.wants/git-maintenance@daily.timer
332 ~/.config/systemd/user/timers.target.wants/git-maintenance@weekly.timer
333 -----------------------------------------------------------------------
335 `git maintenance start` will overwrite these files and start the timer
336 again with `systemctl --user`, so any customization should be done by
337 creating a drop-in file, i.e. a `.conf` suffixed file in the
338 `~/.config/systemd/user/git-maintenance@.service.d` directory.
340 `git maintenance stop` will stop the user systemd timers and delete
341 the above mentioned files.
343 For more details, see `systemd.timer(5)`.
346 BACKGROUND MAINTENANCE ON MACOS SYSTEMS
347 ---------------------------------------
349 While macOS technically supports `cron`, using `crontab -e` requires
350 elevated privileges and the executed process does not have a full user
351 context. Without a full user context, Git and its credential helpers
352 cannot access stored credentials, so some maintenance tasks are not
353 functional.
355 Instead, `git maintenance start` interacts with the `launchctl` tool,
356 which is the recommended way to schedule timed jobs in macOS. Scheduling
357 maintenance through `git maintenance (start|stop)` requires some
358 `launchctl` features available only in macOS 10.11 or later.
360 Your user-specific scheduled tasks are stored as XML-formatted `.plist`
361 files in `~/Library/LaunchAgents/`. You can see the currently-registered
362 tasks using the following command:
364 -----------------------------------------------------------------------
365 $ ls ~/Library/LaunchAgents/org.git-scm.git*
366 org.git-scm.git.daily.plist
367 org.git-scm.git.hourly.plist
368 org.git-scm.git.weekly.plist
369 -----------------------------------------------------------------------
371 One task is registered for each `--schedule=<frequency>` option. To
372 inspect how the XML format describes each schedule, open one of these
373 `.plist` files in an editor and inspect the `<array>` element following
374 the `<key>StartCalendarInterval</key>` element.
376 `git maintenance start` will overwrite these files and register the
377 tasks again with `launchctl`, so any customizations should be done by
378 creating your own `.plist` files with distinct names. Similarly, the
379 `git maintenance stop` command will unregister the tasks with `launchctl`
380 and delete the `.plist` files.
382 To create more advanced customizations to your background tasks, see
383 launchctl.plist(5) for more information.
386 BACKGROUND MAINTENANCE ON WINDOWS SYSTEMS
387 -----------------------------------------
389 Windows does not support `cron` and instead has its own system for
390 scheduling background tasks. The `git maintenance start` command uses
391 the `schtasks` command to submit tasks to this system. You can inspect
392 all background tasks using the Task Scheduler application. The tasks
393 added by Git have names of the form `Git Maintenance (<frequency>)`.
394 The Task Scheduler GUI has ways to inspect these tasks, but you can also
395 export the tasks to XML files and view the details there.
397 Note that since Git is a console application, these background tasks
398 create a console window visible to the current user. This can be changed
399 manually by selecting the "Run whether user is logged in or not" option
400 in Task Scheduler. This change requires a password input, which is why
401 `git maintenance start` does not select it by default.
403 If you want to customize the background tasks, please rename the tasks
404 so future calls to `git maintenance (start|stop)` do not overwrite your
405 custom tasks.
407 CONFIGURATION
408 -------------
410 include::includes/cmd-config-section-all.txt[]
412 include::config/maintenance.txt[]
417 Part of the linkgit:git[1] suite