]> git.ipfire.org Git - thirdparty/git.git/blame - Documentation/githooks.txt
builtin/merge: honor commit-msg hook for merges
[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
34'git init' may copy hooks to the new repository, depending on its
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
de0824ed 48This hook is invoked by 'git am'. It takes a single
6d35cc76 49parameter, the name of the file that holds the proposed commit
de0824ed
ÆAB
50log message. Exiting with a non-zero status causes 'git am' to abort
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
0b444cdb 64This hook is invoked by 'git am'. 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
0b444cdb 79This hook is invoked by 'git am'. 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
0b444cdb 83the outcome of 'git am'.
6d35cc76
JH
84
85pre-commit
6d71c1dc 86~~~~~~~~~~
6d35cc76 87
0b444cdb 88This hook is invoked by 'git commit', 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
ÆAB
91making a commit. Exiting with a non-zero status from this script
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
0b444cdb 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
8089c85b 102prepare-commit-msg
6d71c1dc 103~~~~~~~~~~~~~~~~~~
8089c85b 104
0b444cdb 105This hook is invoked by 'git commit' right after preparing the
8089c85b
PB
106default log message, and before the editor is started.
107
108It takes one to three parameters. The first is the name of the file
99686960 109that contains the commit log message. The second is the source of the commit
5d6b3a9e
SG
110message, and can be: `message` (if a `-m` or `-F` option was
111given); `template` (if a `-t` option was given or the
8089c85b
PB
112configuration option `commit.template` is set); `merge` (if the
113commit is a merge or a `.git/MERGE_MSG` file exists); `squash`
114(if a `.git/SQUASH_MSG` file exists); or `commit`, followed by
d5fa1f1a 115a commit SHA-1 (if a `-c`, `-C` or `--amend` option was given).
8089c85b 116
0b444cdb 117If the exit status is non-zero, 'git commit' will abort.
8089c85b
PB
118
119The purpose of the hook is to edit the message file in place, and
6cf378f0 120it is not suppressed by the `--no-verify` option. A non-zero exit
8089c85b
PB
121means a failure of the hook and aborts the commit. It should not
122be used as replacement for pre-commit hook.
123
0ef1a4e3
KS
124The sample `prepare-commit-msg` hook that comes with Git removes the
125help message found in the commented portion of the commit template.
126
6d35cc76 127commit-msg
6d71c1dc 128~~~~~~~~~~
6d35cc76 129
0b444cdb 130This hook is invoked by 'git commit', and can be bypassed
de0824ed 131with the `--no-verify` option. It takes a single parameter, the
6d35cc76 132name of the file that holds the proposed commit log message.
de0824ed 133Exiting with a non-zero status causes the 'git commit' to
6d35cc76
JH
134abort.
135
de0824ed
ÆAB
136The hook is allowed to edit the message file in place, and can be used
137to normalize the message into some project standard format. It
138can also be used to refuse the commit after inspecting the message
139file.
6d35cc76 140
45ad9b50
JF
141The default 'commit-msg' hook, when enabled, detects duplicate
142"Signed-off-by" lines, and aborts the commit if one is found.
6d35cc76
JH
143
144post-commit
6d71c1dc 145~~~~~~~~~~~
6d35cc76 146
de0824ed
ÆAB
147This hook is invoked by 'git commit'. It takes no parameters, and is
148invoked after a commit is made.
6d35cc76
JH
149
150This hook is meant primarily for notification, and cannot affect
0b444cdb 151the outcome of 'git commit'.
6d35cc76 152
00e5d48a 153pre-rebase
6d71c1dc 154~~~~~~~~~~
00e5d48a 155
0414acc3
TK
156This hook is called by 'git rebase' and can be used to prevent a
157branch from getting rebased. The hook may be called with one or
158two parameters. The first parameter is the upstream from which
159the series was forked. The second parameter is the branch being
160rebased, and is not set when rebasing the current branch.
00e5d48a 161
1abbe475 162post-checkout
6d71c1dc 163~~~~~~~~~~~~~
1abbe475 164
0b444cdb 165This hook is invoked when a 'git checkout' is run after having updated the
1abbe475
JE
166worktree. The hook is given three parameters: the ref of the previous HEAD,
167the ref of the new HEAD (which may or may not have changed), and a flag
168indicating whether the checkout was a branch checkout (changing branches,
169flag=1) or a file checkout (retrieving a file from the index, flag=0).
0b444cdb 170This hook cannot affect the outcome of 'git checkout'.
1abbe475 171
0b444cdb 172It is also run after 'git clone', unless the --no-checkout (-n) option is
24c11552
JL
173used. The first parameter given to the hook is the null-ref, the second the
174ref of the new HEAD and the flag is always 1.
175
1abbe475
JE
176This hook can be used to perform repository validity checks, auto-display
177differences from the previous HEAD if different, or set working dir metadata
178properties.
179
46232915 180post-merge
6d71c1dc 181~~~~~~~~~~
46232915 182
0b444cdb 183This hook is invoked by 'git merge', which happens when a 'git pull'
46232915
JE
184is done on a local repository. The hook takes a single parameter, a status
185flag specifying whether or not the merge being done was a squash merge.
0b444cdb 186This hook cannot affect the outcome of 'git merge' and is not executed,
2b3e60c2 187if the merge failed due to conflicts.
46232915
JE
188
189This hook can be used in conjunction with a corresponding pre-commit hook to
190save and restore any form of metadata associated with the working tree
f745acb0 191(e.g.: permissions/ownership, ACLS, etc). See contrib/hooks/setgitperms.perl
af6fb4c8 192for an example of how to do this.
46232915 193
ec55559f
AS
194pre-push
195~~~~~~~~
196
197This hook is called by 'git push' and can be used to prevent a push from taking
198place. The hook is called with two parameters which provide the name and
199location of the destination remote, if a named remote is not being used both
200values will be the same.
201
202Information about what is to be pushed is provided on the hook's standard
203input with lines of the form:
204
205 <local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF
206
207For instance, if the command +git push origin master:foreign+ were run the
208hook would receive a line like the following:
209
210 refs/heads/master 67890 refs/heads/foreign 12345
211
d5fa1f1a
TA
212although the full, 40-character SHA-1s would be supplied. If the foreign ref
213does not yet exist the `<remote SHA-1>` will be 40 `0`. If a ref is to be
ec55559f 214deleted, the `<local ref>` will be supplied as `(delete)` and the `<local
d5fa1f1a
TA
215SHA-1>` will be 40 `0`. If the local commit was specified by something other
216than a name which could be expanded (such as `HEAD~`, or a SHA-1) it will be
ec55559f
AS
217supplied as it was originally given.
218
219If this hook exits with a non-zero status, 'git push' will abort without
220pushing anything. Information about why the push is rejected may be sent
221to the user by writing to standard error.
222
cbb84e5d
JH
223[[pre-receive]]
224pre-receive
6d71c1dc 225~~~~~~~~~~~
cbb84e5d 226
ba020ef5 227This hook is invoked by 'git-receive-pack' on the remote repository,
0b444cdb 228which happens when a 'git push' is done on a local repository.
cbb84e5d
JH
229Just before starting to update refs on the remote repository, the
230pre-receive hook is invoked. Its exit status determines the success
231or failure of the update.
232
233This hook executes once for the receive operation. It takes no
234arguments, but for each ref to be updated it receives on standard
235input a line of the format:
236
237 <old-value> SP <new-value> SP <ref-name> LF
238
239where `<old-value>` is the old object name stored in the ref,
240`<new-value>` is the new object name to be stored in the ref and
241`<ref-name>` is the full name of the ref.
242When creating a new ref, `<old-value>` is 40 `0`.
243
244If the hook exits with non-zero status, none of the refs will be
245updated. If the hook exits with zero, updating of individual refs can
246still be prevented by the <<update,'update'>> hook.
247
24a0d61e 248Both standard output and standard error output are forwarded to
0b444cdb 249'git send-pack' on the other end, so you can simply `echo` messages
24a0d61e 250for the user.
cbb84e5d 251
77a9745d
SB
252The number of push options given on the command line of
253`git push --push-option=...` can be read from the environment
254variable `GIT_PUSH_OPTION_COUNT`, and the options themselves are
255found in `GIT_PUSH_OPTION_0`, `GIT_PUSH_OPTION_1`,...
256If it is negotiated to not use the push options phase, the
257environment variables will not be set. If the client selects
258to use push options, but doesn't transmit any, the count variable
259will be set to zero, `GIT_PUSH_OPTION_COUNT=0`.
260
eaeed077
JK
261See the section on "Quarantine Environment" in
262linkgit:git-receive-pack[1] for some caveats.
263
cbb84e5d 264[[update]]
6d35cc76 265update
6d71c1dc 266~~~~~~
6d35cc76 267
ba020ef5 268This hook is invoked by 'git-receive-pack' on the remote repository,
0b444cdb 269which happens when a 'git push' is done on a local repository.
6250ad1e 270Just before updating the ref on the remote repository, the update hook
37425065 271is invoked. Its exit status determines the success or failure of
6250ad1e
JL
272the ref update.
273
274The hook executes once for each ref to be updated, and takes
275three parameters:
45ad9b50
JF
276
277 - the name of the ref being updated,
278 - the old object name stored in the ref,
5fe8f49b 279 - and the new object name to be stored in the ref.
6250ad1e
JL
280
281A zero exit from the update hook allows the ref to be updated.
ba020ef5 282Exiting with a non-zero status prevents 'git-receive-pack'
cbb84e5d 283from updating that ref.
6250ad1e
JL
284
285This hook can be used to prevent 'forced' update on certain refs by
6d35cc76
JH
286making sure that the object name is a commit object that is a
287descendant of the commit object named by the old object name.
a75d7b54 288That is, to enforce a "fast-forward only" policy.
6250ad1e
JL
289
290It could also be used to log the old..new status. However, it
291does not know the entire set of branches, so it would end up
cbb84e5d
JH
292firing one e-mail per ref when used naively, though. The
293<<post-receive,'post-receive'>> hook is more suited to that.
6250ad1e 294
bf7d977f
ÆAB
295In an environment that restricts the users' access only to git
296commands over the wire, this hook can be used to implement access
297control without relying on filesystem ownership and group
298membership. See linkgit:git-shell[1] for how you might use the login
299shell to restrict the user's access to only git commands.
6d35cc76 300
24a0d61e 301Both standard output and standard error output are forwarded to
0b444cdb 302'git send-pack' on the other end, so you can simply `echo` messages
24a0d61e 303for the user.
3aadad1b 304
cbb84e5d 305The default 'update' hook, when enabled--and with
39c448c1 306`hooks.allowunannotated` config option unset or set to false--prevents
cbb84e5d
JH
307unannotated tags to be pushed.
308
309[[post-receive]]
310post-receive
6d71c1dc 311~~~~~~~~~~~~
6250ad1e 312
ba020ef5 313This hook is invoked by 'git-receive-pack' on the remote repository,
0b444cdb 314which happens when a 'git push' is done on a local repository.
cbb84e5d
JH
315It executes on the remote repository once after all the refs have
316been updated.
317
318This hook executes once for the receive operation. It takes no
24a0d61e
JH
319arguments, but gets the same information as the
320<<pre-receive,'pre-receive'>>
cbb84e5d
JH
321hook does on its standard input.
322
ba020ef5 323This hook does not affect the outcome of 'git-receive-pack', as it
cbb84e5d
JH
324is called after the real work is done.
325
02783075 326This supersedes the <<post-update,'post-update'>> hook in that it gets
24a0d61e
JH
327both old and new values of all the refs in addition to their
328names.
cbb84e5d 329
24a0d61e 330Both standard output and standard error output are forwarded to
0b444cdb 331'git send-pack' on the other end, so you can simply `echo` messages
24a0d61e 332for the user.
cbb84e5d
JH
333
334The default 'post-receive' hook is empty, but there is
335a sample script `post-receive-email` provided in the `contrib/hooks`
2de9b711 336directory in Git distribution, which implements sending commit
cbb84e5d
JH
337emails.
338
77a9745d
SB
339The number of push options given on the command line of
340`git push --push-option=...` can be read from the environment
341variable `GIT_PUSH_OPTION_COUNT`, and the options themselves are
342found in `GIT_PUSH_OPTION_0`, `GIT_PUSH_OPTION_1`,...
343If it is negotiated to not use the push options phase, the
344environment variables will not be set. If the client selects
345to use push options, but doesn't transmit any, the count variable
346will be set to zero, `GIT_PUSH_OPTION_COUNT=0`.
347
cbb84e5d 348[[post-update]]
6d35cc76 349post-update
6d71c1dc 350~~~~~~~~~~~
6d35cc76 351
ba020ef5 352This hook is invoked by 'git-receive-pack' on the remote repository,
0b444cdb 353which happens when a 'git push' is done on a local repository.
6250ad1e
JL
354It executes on the remote repository once after all the refs have
355been updated.
356
357It takes a variable number of parameters, each of which is the
358name of ref that was actually updated.
6d35cc76
JH
359
360This hook is meant primarily for notification, and cannot affect
ba020ef5 361the outcome of 'git-receive-pack'.
6d35cc76 362
45ad9b50 363The 'post-update' hook can tell what are the heads that were pushed,
6250ad1e 364but it does not know what their original and updated values are,
24a0d61e
JH
365so it is a poor place to do log old..new. The
366<<post-receive,'post-receive'>> hook does get both original and
367updated values of the refs. You might consider it instead if you need
368them.
cbb84e5d 369
45ad9b50 370When enabled, the default 'post-update' hook runs
0b444cdb 371'git update-server-info' to keep the information used by dumb
45ad9b50 372transports (e.g., HTTP) up-to-date. If you are publishing
2de9b711 373a Git repository that is accessible via HTTP, you should
6250ad1e 374probably enable this hook.
3aadad1b 375
cbb84e5d 376Both standard output and standard error output are forwarded to
0b444cdb 377'git send-pack' on the other end, so you can simply `echo` messages
24a0d61e 378for the user.
0b85d926 379
08553319
JH
380push-to-checkout
381~~~~~~~~~~~~~~~~
382
383This hook is invoked by 'git-receive-pack' on the remote repository,
384which happens when a 'git push' is done on a local repository, when
385the push tries to update the branch that is currently checked out
386and the `receive.denyCurrentBranch` configuration variable is set to
387`updateInstead`. Such a push by default is refused if the working
388tree and the index of the remote repository has any difference from
389the currently checked out commit; when both the working tree and the
390index match the current commit, they are updated to match the newly
391pushed tip of the branch. This hook is to be used to override the
392default behaviour.
393
394The hook receives the commit with which the tip of the current
395branch is going to be updated. It can exit with a non-zero status
396to refuse the push (when it does so, it must not modify the index or
397the working tree). Or it can make any necessary changes to the
398working tree and to the index to bring them to the desired state
399when the tip of the current branch is updated to the new commit, and
400exit with a zero status.
401
402For example, the hook can simply run `git read-tree -u -m HEAD "$1"`
403in order to emulate 'git fetch' that is run in the reverse direction
404with `git push`, as the two-tree form of `read-tree -u -m` is
405essentially the same as `git checkout` that switches branches while
406keeping the local changes in the working tree that do not interfere
407with the difference between the branches.
408
409
0b85d926 410pre-auto-gc
6d71c1dc 411~~~~~~~~~~~
0b85d926 412
0b444cdb
TR
413This hook is invoked by 'git gc --auto'. It takes no parameter, and
414exiting with non-zero status from this script causes the 'git gc --auto'
0b85d926 415to abort.
a5af0e2c 416
c0fc6869
TR
417post-rewrite
418~~~~~~~~~~~~
419
420This hook is invoked by commands that rewrite commits (`git commit
421--amend`, 'git-rebase'; currently 'git-filter-branch' does 'not' call
422it!). Its first argument denotes the command it was invoked by:
423currently one of `amend` or `rebase`. Further command-dependent
424arguments may be passed in the future.
425
426The hook receives a list of the rewritten commits on stdin, in the
427format
428
429 <old-sha1> SP <new-sha1> [ SP <extra-info> ] LF
430
431The 'extra-info' is again command-dependent. If it is empty, the
432preceding SP is also omitted. Currently, no commands pass any
433'extra-info'.
434
6956f858 435The hook always runs after the automatic note copying (see
ad52148a 436"notes.rewrite.<command>" in linkgit:git-config[1]) has happened, and
6956f858
TR
437thus has access to these notes.
438
c0fc6869
TR
439The following command-specific comments apply:
440
441rebase::
442 For the 'squash' and 'fixup' operation, all commits that were
443 squashed are listed as being rewritten to the squashed commit.
444 This means that there will be several lines sharing the same
445 'new-sha1'.
446+
447The commits are guaranteed to be listed in the order that they were
448processed by rebase.
449
6489660b
JT
450sendemail-validate
451~~~~~~~~~~~~~~~~~~
452
453This hook is invoked by 'git send-email'. It takes a single parameter,
454the name of the file that holds the e-mail to be sent. Exiting with a
455non-zero status causes 'git send-email' to abort before sending any
456e-mails.
457
c0fc6869 458
a5af0e2c
CC
459GIT
460---
9e1f0a85 461Part of the linkgit:git[1] suite