]> git.ipfire.org Git - thirdparty/git.git/blame - Documentation/githooks.txt
path.c: clarify trie_find()'s in-code comment
[thirdparty/git.git] / Documentation / githooks.txt
CommitLineData
a5af0e2c
CC
1githooks(5)
2===========
3
4NAME
5----
2de9b711 6githooks - Hooks used by Git
a5af0e2c
CC
7
8SYNOPSIS
9--------
867ad08a 10$GIT_DIR/hooks/* (or \`git config core.hooksPath`/*)
a5af0e2c
CC
11
12
13DESCRIPTION
14-----------
6d35cc76 15
867ad08a
ÆAB
16Hooks are programs you can place in a hooks directory to trigger
17actions at certain points in git's execution. Hooks that don't have
18the executable bit set are ignored.
19
20By default the hooks directory is `$GIT_DIR/hooks`, but that can be
21changed via the `core.hooksPath` configuration variable (see
22linkgit:git-config[1]).
49fa52fd
ÆAB
23
24Before Git invokes a hook, it changes its working directory to either
501d3cd7
SR
25$GIT_DIR in a bare repository or the root of the working tree in a non-bare
26repository. An exception are hooks triggered during a push ('pre-receive',
27'update', 'post-receive', 'post-update', 'push-to-checkout') which are always
28executed in $GIT_DIR.
49fa52fd
ÆAB
29
30Hooks can get their arguments via the environment, command-line
31arguments, and stdin. See the documentation for each hook below for
32details.
33
9dba84d8 34`git init` may copy hooks to the new repository, depending on its
49fa52fd
ÆAB
35configuration. See the "TEMPLATE DIRECTORY" section in
36linkgit:git-init[1] for details. When the rest of this document refers
37to "default hooks" it's talking about the default template shipped
38with Git.
39
40The currently supported hooks are described below.
6d35cc76 41
6d71c1dc
BW
42HOOKS
43-----
44
6d35cc76 45applypatch-msg
6d71c1dc 46~~~~~~~~~~~~~~
6d35cc76 47
9dba84d8 48This hook is invoked by linkgit:git-am[1]. It takes a single
6d35cc76 49parameter, the name of the file that holds the proposed commit
9dba84d8 50log message. Exiting with a non-zero status causes `git am` to abort
de0824ed 51before applying the patch.
6d35cc76
JH
52
53The hook is allowed to edit the message file in place, and can
54be used to normalize the message into some project standard
de0824ed
ÆAB
55format. It can also be used to refuse the commit after inspecting
56the message file.
6d35cc76 57
45ad9b50
JF
58The default 'applypatch-msg' hook, when enabled, runs the
59'commit-msg' hook, if the latter is enabled.
6d35cc76
JH
60
61pre-applypatch
6d71c1dc 62~~~~~~~~~~~~~~
6d35cc76 63
9dba84d8 64This hook is invoked by linkgit:git-am[1]. It takes no parameter, and is
47458bb9
CC
65invoked after the patch is applied, but before a commit is made.
66
67If it exits with non-zero status, then the working tree will not be
68committed after applying the patch.
6d35cc76
JH
69
70It can be used to inspect the current working tree and refuse to
71make a commit if it does not pass certain test.
72
45ad9b50
JF
73The default 'pre-applypatch' hook, when enabled, runs the
74'pre-commit' hook, if the latter is enabled.
6d35cc76
JH
75
76post-applypatch
6d71c1dc 77~~~~~~~~~~~~~~~
6d35cc76 78
9dba84d8 79This hook is invoked by linkgit:git-am[1]. It takes no parameter,
6d35cc76
JH
80and is invoked after the patch is applied and a commit is made.
81
82This hook is meant primarily for notification, and cannot affect
9dba84d8 83the outcome of `git am`.
6d35cc76
JH
84
85pre-commit
6d71c1dc 86~~~~~~~~~~
6d35cc76 87
9dba84d8 88This hook is invoked by linkgit:git-commit[1], and can be bypassed
de0824ed 89with the `--no-verify` option. It takes no parameters, and is
6d35cc76 90invoked before obtaining the proposed commit log message and
de0824ed 91making a commit. Exiting with a non-zero status from this script
9dba84d8 92causes the `git commit` command to abort before creating a commit.
6d35cc76 93
45ad9b50 94The default 'pre-commit' hook, when enabled, catches introduction
6d35cc76 95of lines with trailing whitespaces and aborts the commit when
45ad9b50 96such a line is found.
6d35cc76 97
9dba84d8 98All the `git commit` hooks are invoked with the environment
406400ce
PB
99variable `GIT_EDITOR=:` if the command will not bring up an editor
100to modify the commit message.
101
3e14dd2c
RD
102The default 'pre-commit' hook, when enabled--and with the
103`hooks.allownonascii` config option unset or set to false--prevents
104the use of non-ASCII filenames.
105
8089c85b 106prepare-commit-msg
6d71c1dc 107~~~~~~~~~~~~~~~~~~
8089c85b 108
9dba84d8 109This hook is invoked by linkgit:git-commit[1] right after preparing the
8089c85b
PB
110default log message, and before the editor is started.
111
112It takes one to three parameters. The first is the name of the file
99686960 113that contains the commit log message. The second is the source of the commit
5d6b3a9e
SG
114message, and can be: `message` (if a `-m` or `-F` option was
115given); `template` (if a `-t` option was given or the
8089c85b
PB
116configuration option `commit.template` is set); `merge` (if the
117commit is a merge or a `.git/MERGE_MSG` file exists); `squash`
118(if a `.git/SQUASH_MSG` file exists); or `commit`, followed by
d5fa1f1a 119a commit SHA-1 (if a `-c`, `-C` or `--amend` option was given).
8089c85b 120
9dba84d8 121If the exit status is non-zero, `git commit` will abort.
8089c85b
PB
122
123The purpose of the hook is to edit the message file in place, and
6cf378f0 124it is not suppressed by the `--no-verify` option. A non-zero exit
8089c85b
PB
125means a failure of the hook and aborts the commit. It should not
126be used as replacement for pre-commit hook.
127
0ef1a4e3
KS
128The sample `prepare-commit-msg` hook that comes with Git removes the
129help message found in the commented portion of the commit template.
130
6d35cc76 131commit-msg
6d71c1dc 132~~~~~~~~~~
6d35cc76 133
9dba84d8 134This hook is invoked by linkgit:git-commit[1] and linkgit:git-merge[1], and can be
ce82eddf
SB
135bypassed with the `--no-verify` option. It takes a single parameter,
136the name of the file that holds the proposed commit log message.
137Exiting with a non-zero status causes the command to abort.
6d35cc76 138
de0824ed
ÆAB
139The hook is allowed to edit the message file in place, and can be used
140to normalize the message into some project standard format. It
141can also be used to refuse the commit after inspecting the message
142file.
6d35cc76 143
45ad9b50
JF
144The default 'commit-msg' hook, when enabled, detects duplicate
145"Signed-off-by" lines, and aborts the commit if one is found.
6d35cc76
JH
146
147post-commit
6d71c1dc 148~~~~~~~~~~~
6d35cc76 149
9dba84d8 150This hook is invoked by linkgit:git-commit[1]. It takes no parameters, and is
de0824ed 151invoked after a commit is made.
6d35cc76
JH
152
153This hook is meant primarily for notification, and cannot affect
9dba84d8 154the outcome of `git commit`.
6d35cc76 155
00e5d48a 156pre-rebase
6d71c1dc 157~~~~~~~~~~
00e5d48a 158
9dba84d8 159This hook is called by linkgit:git-rebase[1] and can be used to prevent a
0414acc3
TK
160branch from getting rebased. The hook may be called with one or
161two parameters. The first parameter is the upstream from which
162the series was forked. The second parameter is the branch being
163rebased, and is not set when rebasing the current branch.
00e5d48a 164
1abbe475 165post-checkout
6d71c1dc 166~~~~~~~~~~~~~
1abbe475 167
d787d311
NTND
168This hook is invoked when a linkgit:git-checkout[1] or
169linkgit:git-switch[1] is run after having updated the
1abbe475
JE
170worktree. The hook is given three parameters: the ref of the previous HEAD,
171the ref of the new HEAD (which may or may not have changed), and a flag
172indicating whether the checkout was a branch checkout (changing branches,
173flag=1) or a file checkout (retrieving a file from the index, flag=0).
d787d311 174This hook cannot affect the outcome of `git switch` or `git checkout`.
1abbe475 175
9dba84d8 176It is also run after linkgit:git-clone[1], unless the `--no-checkout` (`-n`) option is
24c11552 177used. The first parameter given to the hook is the null-ref, the second the
9dba84d8
AH
178ref of the new HEAD and the flag is always 1. Likewise for `git worktree add`
179unless `--no-checkout` is used.
24c11552 180
1abbe475
JE
181This hook can be used to perform repository validity checks, auto-display
182differences from the previous HEAD if different, or set working dir metadata
183properties.
184
46232915 185post-merge
6d71c1dc 186~~~~~~~~~~
46232915 187
9dba84d8 188This hook is invoked by linkgit:git-merge[1], which happens when a `git pull`
46232915
JE
189is done on a local repository. The hook takes a single parameter, a status
190flag specifying whether or not the merge being done was a squash merge.
9dba84d8 191This hook cannot affect the outcome of `git merge` and is not executed,
2b3e60c2 192if the merge failed due to conflicts.
46232915
JE
193
194This hook can be used in conjunction with a corresponding pre-commit hook to
195save and restore any form of metadata associated with the working tree
f745acb0 196(e.g.: permissions/ownership, ACLS, etc). See contrib/hooks/setgitperms.perl
af6fb4c8 197for an example of how to do this.
46232915 198
ec55559f
AS
199pre-push
200~~~~~~~~
201
9dba84d8
AH
202This hook is called by linkgit:git-push[1] and can be used to prevent
203a push from taking place. The hook is called with two parameters
204which provide the name and location of the destination remote, if a
205named remote is not being used both values will be the same.
ec55559f
AS
206
207Information about what is to be pushed is provided on the hook's standard
208input with lines of the form:
209
210 <local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF
211
212For instance, if the command +git push origin master:foreign+ were run the
213hook would receive a line like the following:
214
215 refs/heads/master 67890 refs/heads/foreign 12345
216
d5fa1f1a
TA
217although the full, 40-character SHA-1s would be supplied. If the foreign ref
218does not yet exist the `<remote SHA-1>` will be 40 `0`. If a ref is to be
ec55559f 219deleted, the `<local ref>` will be supplied as `(delete)` and the `<local
d5fa1f1a
TA
220SHA-1>` will be 40 `0`. If the local commit was specified by something other
221than a name which could be expanded (such as `HEAD~`, or a SHA-1) it will be
ec55559f
AS
222supplied as it was originally given.
223
9dba84d8 224If this hook exits with a non-zero status, `git push` will abort without
ec55559f
AS
225pushing anything. Information about why the push is rejected may be sent
226to the user by writing to standard error.
227
cbb84e5d
JH
228[[pre-receive]]
229pre-receive
6d71c1dc 230~~~~~~~~~~~
cbb84e5d 231
9dba84d8
AH
232This hook is invoked by linkgit:git-receive-pack[1] when it reacts to
233`git push` and updates reference(s) in its repository.
cbb84e5d
JH
234Just before starting to update refs on the remote repository, the
235pre-receive hook is invoked. Its exit status determines the success
236or failure of the update.
237
238This hook executes once for the receive operation. It takes no
239arguments, but for each ref to be updated it receives on standard
240input a line of the format:
241
242 <old-value> SP <new-value> SP <ref-name> LF
243
244where `<old-value>` is the old object name stored in the ref,
245`<new-value>` is the new object name to be stored in the ref and
246`<ref-name>` is the full name of the ref.
247When creating a new ref, `<old-value>` is 40 `0`.
248
249If the hook exits with non-zero status, none of the refs will be
250updated. If the hook exits with zero, updating of individual refs can
251still be prevented by the <<update,'update'>> hook.
252
24a0d61e 253Both standard output and standard error output are forwarded to
9dba84d8 254`git send-pack` on the other end, so you can simply `echo` messages
24a0d61e 255for the user.
cbb84e5d 256
77a9745d
SB
257The number of push options given on the command line of
258`git push --push-option=...` can be read from the environment
259variable `GIT_PUSH_OPTION_COUNT`, and the options themselves are
260found in `GIT_PUSH_OPTION_0`, `GIT_PUSH_OPTION_1`,...
261If it is negotiated to not use the push options phase, the
262environment variables will not be set. If the client selects
263to use push options, but doesn't transmit any, the count variable
264will be set to zero, `GIT_PUSH_OPTION_COUNT=0`.
265
eaeed077
JK
266See the section on "Quarantine Environment" in
267linkgit:git-receive-pack[1] for some caveats.
268
cbb84e5d 269[[update]]
6d35cc76 270update
6d71c1dc 271~~~~~~
6d35cc76 272
9dba84d8
AH
273This hook is invoked by linkgit:git-receive-pack[1] when it reacts to
274`git push` and updates reference(s) in its repository.
6250ad1e 275Just before updating the ref on the remote repository, the update hook
37425065 276is invoked. Its exit status determines the success or failure of
6250ad1e
JL
277the ref update.
278
279The hook executes once for each ref to be updated, and takes
280three parameters:
45ad9b50
JF
281
282 - the name of the ref being updated,
283 - the old object name stored in the ref,
5fe8f49b 284 - and the new object name to be stored in the ref.
6250ad1e
JL
285
286A zero exit from the update hook allows the ref to be updated.
9dba84d8 287Exiting with a non-zero status prevents `git receive-pack`
cbb84e5d 288from updating that ref.
6250ad1e
JL
289
290This hook can be used to prevent 'forced' update on certain refs by
6d35cc76
JH
291making sure that the object name is a commit object that is a
292descendant of the commit object named by the old object name.
a75d7b54 293That is, to enforce a "fast-forward only" policy.
6250ad1e
JL
294
295It could also be used to log the old..new status. However, it
296does not know the entire set of branches, so it would end up
cbb84e5d
JH
297firing one e-mail per ref when used naively, though. The
298<<post-receive,'post-receive'>> hook is more suited to that.
6250ad1e 299
bf7d977f
ÆAB
300In an environment that restricts the users' access only to git
301commands over the wire, this hook can be used to implement access
302control without relying on filesystem ownership and group
303membership. See linkgit:git-shell[1] for how you might use the login
304shell to restrict the user's access to only git commands.
6d35cc76 305
24a0d61e 306Both standard output and standard error output are forwarded to
9dba84d8 307`git send-pack` on the other end, so you can simply `echo` messages
24a0d61e 308for the user.
3aadad1b 309
cbb84e5d 310The default 'update' hook, when enabled--and with
39c448c1 311`hooks.allowunannotated` config option unset or set to false--prevents
cbb84e5d
JH
312unannotated tags to be pushed.
313
314[[post-receive]]
315post-receive
6d71c1dc 316~~~~~~~~~~~~
6250ad1e 317
9dba84d8
AH
318This hook is invoked by linkgit:git-receive-pack[1] when it reacts to
319`git push` and updates reference(s) in its repository.
cbb84e5d
JH
320It executes on the remote repository once after all the refs have
321been updated.
322
323This hook executes once for the receive operation. It takes no
24a0d61e
JH
324arguments, but gets the same information as the
325<<pre-receive,'pre-receive'>>
cbb84e5d
JH
326hook does on its standard input.
327
9dba84d8 328This hook does not affect the outcome of `git receive-pack`, as it
cbb84e5d
JH
329is called after the real work is done.
330
02783075 331This supersedes the <<post-update,'post-update'>> hook in that it gets
24a0d61e
JH
332both old and new values of all the refs in addition to their
333names.
cbb84e5d 334
24a0d61e 335Both standard output and standard error output are forwarded to
9dba84d8 336`git send-pack` on the other end, so you can simply `echo` messages
24a0d61e 337for the user.
cbb84e5d
JH
338
339The default 'post-receive' hook is empty, but there is
340a sample script `post-receive-email` provided in the `contrib/hooks`
2de9b711 341directory in Git distribution, which implements sending commit
cbb84e5d
JH
342emails.
343
77a9745d
SB
344The number of push options given on the command line of
345`git push --push-option=...` can be read from the environment
346variable `GIT_PUSH_OPTION_COUNT`, and the options themselves are
347found in `GIT_PUSH_OPTION_0`, `GIT_PUSH_OPTION_1`,...
348If it is negotiated to not use the push options phase, the
349environment variables will not be set. If the client selects
350to use push options, but doesn't transmit any, the count variable
351will be set to zero, `GIT_PUSH_OPTION_COUNT=0`.
352
cbb84e5d 353[[post-update]]
6d35cc76 354post-update
6d71c1dc 355~~~~~~~~~~~
6d35cc76 356
9dba84d8
AH
357This hook is invoked by linkgit:git-receive-pack[1] when it reacts to
358`git push` and updates reference(s) in its repository.
6250ad1e
JL
359It executes on the remote repository once after all the refs have
360been updated.
361
362It takes a variable number of parameters, each of which is the
363name of ref that was actually updated.
6d35cc76
JH
364
365This hook is meant primarily for notification, and cannot affect
9dba84d8 366the outcome of `git receive-pack`.
6d35cc76 367
45ad9b50 368The 'post-update' hook can tell what are the heads that were pushed,
6250ad1e 369but it does not know what their original and updated values are,
24a0d61e
JH
370so it is a poor place to do log old..new. The
371<<post-receive,'post-receive'>> hook does get both original and
372updated values of the refs. You might consider it instead if you need
373them.
cbb84e5d 374
45ad9b50 375When enabled, the default 'post-update' hook runs
9dba84d8 376`git update-server-info` to keep the information used by dumb
7560f547 377transports (e.g., HTTP) up to date. If you are publishing
2de9b711 378a Git repository that is accessible via HTTP, you should
6250ad1e 379probably enable this hook.
3aadad1b 380
cbb84e5d 381Both standard output and standard error output are forwarded to
9dba84d8 382`git send-pack` on the other end, so you can simply `echo` messages
24a0d61e 383for the user.
0b85d926 384
08553319
JH
385push-to-checkout
386~~~~~~~~~~~~~~~~
387
9dba84d8
AH
388This hook is invoked by linkgit:git-receive-pack[1] when it reacts to
389`git push` and updates reference(s) in its repository, and when
08553319
JH
390the push tries to update the branch that is currently checked out
391and the `receive.denyCurrentBranch` configuration variable is set to
392`updateInstead`. Such a push by default is refused if the working
393tree and the index of the remote repository has any difference from
394the currently checked out commit; when both the working tree and the
395index match the current commit, they are updated to match the newly
396pushed tip of the branch. This hook is to be used to override the
397default behaviour.
398
399The hook receives the commit with which the tip of the current
400branch is going to be updated. It can exit with a non-zero status
401to refuse the push (when it does so, it must not modify the index or
402the working tree). Or it can make any necessary changes to the
403working tree and to the index to bring them to the desired state
404when the tip of the current branch is updated to the new commit, and
405exit with a zero status.
406
407For example, the hook can simply run `git read-tree -u -m HEAD "$1"`
9dba84d8
AH
408in order to emulate `git fetch` that is run in the reverse direction
409with `git push`, as the two-tree form of `git read-tree -u -m` is
d787d311
NTND
410essentially the same as `git switch` or `git checkout`
411that switches branches while
08553319
JH
412keeping the local changes in the working tree that do not interfere
413with the difference between the branches.
414
415
0b85d926 416pre-auto-gc
6d71c1dc 417~~~~~~~~~~~
0b85d926 418
9dba84d8
AH
419This hook is invoked by `git gc --auto` (see linkgit:git-gc[1]). It
420takes no parameter, and exiting with non-zero status from this script
421causes the `git gc --auto` to abort.
a5af0e2c 422
c0fc6869
TR
423post-rewrite
424~~~~~~~~~~~~
425
9dba84d8
AH
426This hook is invoked by commands that rewrite commits
427(linkgit:git-commit[1] when called with `--amend` and
428linkgit:git-rebase[1]; currently `git filter-branch` does 'not' call
c0fc6869
TR
429it!). Its first argument denotes the command it was invoked by:
430currently one of `amend` or `rebase`. Further command-dependent
431arguments may be passed in the future.
432
433The hook receives a list of the rewritten commits on stdin, in the
434format
435
436 <old-sha1> SP <new-sha1> [ SP <extra-info> ] LF
437
438The 'extra-info' is again command-dependent. If it is empty, the
439preceding SP is also omitted. Currently, no commands pass any
440'extra-info'.
441
6956f858 442The hook always runs after the automatic note copying (see
ad52148a 443"notes.rewrite.<command>" in linkgit:git-config[1]) has happened, and
6956f858
TR
444thus has access to these notes.
445
c0fc6869
TR
446The following command-specific comments apply:
447
448rebase::
449 For the 'squash' and 'fixup' operation, all commits that were
450 squashed are listed as being rewritten to the squashed commit.
451 This means that there will be several lines sharing the same
452 'new-sha1'.
453+
454The commits are guaranteed to be listed in the order that they were
455processed by rebase.
456
6489660b
JT
457sendemail-validate
458~~~~~~~~~~~~~~~~~~
459
9dba84d8 460This hook is invoked by linkgit:git-send-email[1]. It takes a single parameter,
6489660b 461the name of the file that holds the e-mail to be sent. Exiting with a
9dba84d8 462non-zero status causes `git send-email` to abort before sending any
6489660b
JT
463e-mails.
464
780494b1
BP
465fsmonitor-watchman
466~~~~~~~~~~~~~~~~~~
467
9dba84d8
AH
468This hook is invoked when the configuration option `core.fsmonitor` is
469set to `.git/hooks/fsmonitor-watchman`. It takes two arguments, a version
780494b1
BP
470(currently 1) and the time in elapsed nanoseconds since midnight,
471January 1, 1970.
472
473The hook should output to stdout the list of all files in the working
474directory that may have changed since the requested time. The logic
475should be inclusive so that it does not miss any potential changes.
476The paths should be relative to the root of the working directory
477and be separated by a single NUL.
478
479It is OK to include files which have not actually changed. All changes
480including newly-created and deleted files should be included. When
481files are renamed, both the old and the new name should be included.
482
483Git will limit what files it checks for changes as well as which
484directories are checked for untracked files based on the path names
485given.
486
487An optimized way to tell git "all files have changed" is to return
9dba84d8 488the filename `/`.
780494b1
BP
489
490The exit status determines whether git will use the data from the
491hook to limit its search. On error, it will fall back to verifying
492all files and folders.
c0fc6869 493
251c8c50
CB
494p4-pre-submit
495~~~~~~~~~~~~~
496
497This hook is invoked by `git-p4 submit`. It takes no parameters and nothing
498from standard input. Exiting with non-zero status from this script prevent
499`git-p4 submit` from launching. Run `git-p4 submit --help` for details.
500
1956ecd0
BP
501post-index-change
502~~~~~~~~~~~~~~~~~
503
504This hook is invoked when the index is written in read-cache.c
505do_write_locked_index.
506
507The first parameter passed to the hook is the indicator for the
508working directory being updated. "1" meaning working directory
509was updated or "0" when the working directory was not updated.
510
511The second parameter passed to the hook is the indicator for whether
512or not the index was updated and the skip-worktree bit could have
513changed. "1" meaning skip-worktree bits could have been updated
514and "0" meaning they were not.
515
516Only one parameter should be set to "1" when the hook runs. The hook
517running passing "1", "1" should not be possible.
518
a5af0e2c
CC
519GIT
520---
9e1f0a85 521Part of the linkgit:git[1] suite