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