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