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