1GITHOOKS(5) Git Manual GITHOOKS(5)
2
3
4
6 githooks - Hooks used by Git
7
9 $GIT_DIR/hooks/* (or `git config core.hooksPath`/*)
10
12 Hooks are programs you can place in a hooks directory to trigger
13 actions at certain points in git’s execution. Hooks that don’t have the
14 executable bit set are ignored.
15
16 By default the hooks directory is $GIT_DIR/hooks, but that can be
17 changed via the core.hooksPath configuration variable (see git-
18 config(1)).
19
20 Before Git invokes a hook, it changes its working directory to either
21 $GIT_DIR in a bare repository or the root of the working tree in a
22 non-bare repository. An exception are hooks triggered during a push
23 (pre-receive, update, post-receive, post-update, push-to-checkout)
24 which are always executed in $GIT_DIR.
25
26 Hooks can get their arguments via the environment, command-line
27 arguments, and stdin. See the documentation for each hook below for
28 details.
29
30 git init may copy hooks to the new repository, depending on its
31 configuration. See the "TEMPLATE DIRECTORY" section in git-init(1) for
32 details. When the rest of this document refers to "default hooks" it’s
33 talking about the default template shipped with Git.
34
35 The currently supported hooks are described below.
36
38 applypatch-msg
39 This hook is invoked by git-am(1). It takes a single parameter, the
40 name of the file that holds the proposed commit log message. Exiting
41 with a non-zero status causes git am to abort before applying the
42 patch.
43
44 The hook is allowed to edit the message file in place, and can be used
45 to normalize the message into some project standard format. It can also
46 be used to refuse the commit after inspecting the message file.
47
48 The default applypatch-msg hook, when enabled, runs the commit-msg
49 hook, if the latter is enabled.
50
51 pre-applypatch
52 This hook is invoked by git-am(1). It takes no parameter, and is
53 invoked after the patch is applied, but before a commit is made.
54
55 If it exits with non-zero status, then the working tree will not be
56 committed after applying the patch.
57
58 It can be used to inspect the current working tree and refuse to make a
59 commit if it does not pass certain test.
60
61 The default pre-applypatch hook, when enabled, runs the pre-commit
62 hook, if the latter is enabled.
63
64 post-applypatch
65 This hook is invoked by git-am(1). It takes no parameter, and is
66 invoked after the patch is applied and a commit is made.
67
68 This hook is meant primarily for notification, and cannot affect the
69 outcome of git am.
70
71 pre-commit
72 This hook is invoked by git-commit(1), and can be bypassed with the
73 --no-verify option. It takes no parameters, and is invoked before
74 obtaining the proposed commit log message and making a commit. Exiting
75 with a non-zero status from this script causes the git commit command
76 to abort before creating a commit.
77
78 The default pre-commit hook, when enabled, catches introduction of
79 lines with trailing whitespaces and aborts the commit when such a line
80 is found.
81
82 All the git commit hooks are invoked with the environment variable
83 GIT_EDITOR=: if the command will not bring up an editor to modify the
84 commit message.
85
86 The default pre-commit hook, when enabled—and with the
87 hooks.allownonascii config option unset or set to false—prevents the
88 use of non-ASCII filenames.
89
90 pre-merge-commit
91 This hook is invoked by git-merge(1), and can be bypassed with the
92 --no-verify option. It takes no parameters, and is invoked after the
93 merge has been carried out successfully and before obtaining the
94 proposed commit log message to make a commit. Exiting with a non-zero
95 status from this script causes the git merge command to abort before
96 creating a commit.
97
98 The default pre-merge-commit hook, when enabled, runs the pre-commit
99 hook, if the latter is enabled.
100
101 This hook is invoked with the environment variable GIT_EDITOR=: if the
102 command will not bring up an editor to modify the commit message.
103
104 If the merge cannot be carried out automatically, the conflicts need to
105 be resolved and the result committed separately (see git-merge(1)). At
106 that point, this hook will not be executed, but the pre-commit hook
107 will, if it is enabled.
108
109 prepare-commit-msg
110 This hook is invoked by git-commit(1) right after preparing the default
111 log message, and before the editor is started.
112
113 It takes one to three parameters. The first is the name of the file
114 that contains the commit log message. The second is the source of the
115 commit message, and can be: message (if a -m or -F option was given);
116 template (if a -t option was given or the configuration option
117 commit.template is set); merge (if the commit is a merge or a
118 .git/MERGE_MSG file exists); squash (if a .git/SQUASH_MSG file exists);
119 or commit, followed by a commit SHA-1 (if a -c, -C or --amend option
120 was given).
121
122 If the exit status is non-zero, git commit will abort.
123
124 The purpose of the hook is to edit the message file in place, and it is
125 not suppressed by the --no-verify option. A non-zero exit means a
126 failure of the hook and aborts the commit. It should not be used as
127 replacement for pre-commit hook.
128
129 The sample prepare-commit-msg hook that comes with Git removes the help
130 message found in the commented portion of the commit template.
131
132 commit-msg
133 This hook is invoked by git-commit(1) and git-merge(1), and can be
134 bypassed with the --no-verify option. It takes a single parameter, the
135 name of the file that holds the proposed commit log message. Exiting
136 with a non-zero status causes the command to abort.
137
138 The hook is allowed to edit the message file in place, and can be used
139 to normalize the message into some project standard format. It can also
140 be used to refuse the commit after inspecting the message file.
141
142 The default commit-msg hook, when enabled, detects duplicate
143 Signed-off-by trailers, and aborts the commit if one is found.
144
145 post-commit
146 This hook is invoked by git-commit(1). It takes no parameters, and is
147 invoked after a commit is made.
148
149 This hook is meant primarily for notification, and cannot affect the
150 outcome of git commit.
151
152 pre-rebase
153 This hook is called by git-rebase(1) and can be used to prevent a
154 branch from getting rebased. The hook may be called with one or two
155 parameters. The first parameter is the upstream from which the series
156 was forked. The second parameter is the branch being rebased, and is
157 not set when rebasing the current branch.
158
159 post-checkout
160 This hook is invoked when a git-checkout(1) or git-switch(1) is run
161 after having updated the worktree. The hook is given three parameters:
162 the ref of the previous HEAD, the ref of the new HEAD (which may or may
163 not have changed), and a flag indicating whether the checkout was a
164 branch checkout (changing branches, flag=1) or a file checkout
165 (retrieving a file from the index, flag=0). This hook cannot affect the
166 outcome of git switch or git checkout, other than that the hook’s exit
167 status becomes the exit status of these two commands.
168
169 It is also run after git-clone(1), unless the --no-checkout (-n) option
170 is used. The first parameter given to the hook is the null-ref, the
171 second the ref of the new HEAD and the flag is always 1. Likewise for
172 git worktree add unless --no-checkout is used.
173
174 This hook can be used to perform repository validity checks,
175 auto-display differences from the previous HEAD if different, or set
176 working dir metadata properties.
177
178 post-merge
179 This hook is invoked by git-merge(1), which happens when a git pull is
180 done on a local repository. The hook takes a single parameter, a status
181 flag specifying whether or not the merge being done was a squash merge.
182 This hook cannot affect the outcome of git merge and is not executed,
183 if the merge failed due to conflicts.
184
185 This hook can be used in conjunction with a corresponding pre-commit
186 hook to save and restore any form of metadata associated with the
187 working tree (e.g.: permissions/ownership, ACLS, etc). See
188 contrib/hooks/setgitperms.perl for an example of how to do this.
189
190 pre-push
191 This hook is called by git-push(1) and can be used to prevent a push
192 from taking place. The hook is called with two parameters which provide
193 the name and location of the destination remote, if a named remote is
194 not being used both values will be the same.
195
196 Information about what is to be pushed is provided on the hook’s
197 standard 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
202 the hook would receive a line like the following:
203
204 refs/heads/master 67890 refs/heads/foreign 12345
205
206 although the full, 40-character SHA-1s would be supplied. If the
207 foreign ref does not yet exist the <remote SHA-1> will be 40 0. If a
208 ref is to be deleted, the <local ref> will be supplied as (delete) and
209 the <local SHA-1> will be 40 0. If the local commit was specified by
210 something other than a name which could be expanded (such as HEAD~, or
211 a SHA-1) it will be 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
215 sent to the user by writing to standard error.
216
217 pre-receive
218 This hook is invoked by git-receive-pack(1) when it reacts to git push
219 and updates reference(s) in its repository. Just before starting to
220 update refs on the remote repository, the pre-receive hook is invoked.
221 Its exit status determines the success or failure of the update.
222
223 This hook executes once for the receive operation. It takes no
224 arguments, but for each ref to be updated it receives on standard input
225 a line of the format:
226
227 <old-value> SP <new-value> SP <ref-name> LF
228
229 where <old-value> is the old object name stored in the ref, <new-value>
230 is the new object name to be stored in the ref and <ref-name> is the
231 full name of the ref. When creating a new ref, <old-value> is 40 0.
232
233 If the hook exits with non-zero status, none of the refs will be
234 updated. If the hook exits with zero, updating of individual refs can
235 still be prevented by the update hook.
236
237 Both standard output and standard error output are forwarded to git
238 send-pack on the other end, so you can simply echo messages for the
239 user.
240
241 The number of push options given on the command line of git push
242 --push-option=... can be read from the environment variable
243 GIT_PUSH_OPTION_COUNT, and the options themselves are found in
244 GIT_PUSH_OPTION_0, GIT_PUSH_OPTION_1,... If it is negotiated to not use
245 the push options phase, the environment variables will not be set. If
246 the client selects to use push options, but doesn’t transmit any, the
247 count variable will be set to zero, GIT_PUSH_OPTION_COUNT=0.
248
249 See the section on "Quarantine Environment" in git-receive-pack(1) for
250 some caveats.
251
252 update
253 This hook is invoked by git-receive-pack(1) when it reacts to git push
254 and updates reference(s) in its repository. Just before updating the
255 ref on the remote repository, the update hook is invoked. Its exit
256 status determines the success or failure of the ref update.
257
258 The hook executes once for each ref to be updated, and takes three
259 parameters:
260
261 • the name of the ref being updated,
262
263 • the old object name stored in the ref,
264
265 • and the new object name to be stored in the ref.
266
267 A zero exit from the update hook allows the ref to be updated. Exiting
268 with a non-zero status prevents git receive-pack from updating that
269 ref.
270
271 This hook can be used to prevent forced update on certain refs by
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. That is,
274 to enforce a "fast-forward only" policy.
275
276 It could also be used to log the old..new status. However, it does not
277 know the entire set of branches, so it would end up firing one e-mail
278 per ref when used naively, though. The post-receive hook is more suited
279 to that.
280
281 In an environment that restricts the users' access only to git commands
282 over the wire, this hook can be used to implement access control
283 without relying on filesystem ownership and group membership. See git-
284 shell(1) for how you might use the login shell to restrict the user’s
285 access to only git commands.
286
287 Both standard output and standard error output are forwarded to git
288 send-pack on the other end, so you can simply echo messages for the
289 user.
290
291 The default update hook, when enabled—and with hooks.allowunannotated
292 config option unset or set to false—prevents unannotated tags to be
293 pushed.
294
295 proc-receive
296 This hook is invoked by git-receive-pack(1). If the server has set the
297 multi-valued config variable receive.procReceiveRefs, and the commands
298 sent to receive-pack have matching reference names, these commands will
299 be executed by this hook, instead of by the internal execute_commands()
300 function. This hook is responsible for updating the relevant references
301 and reporting the results back to receive-pack.
302
303 This hook executes once for the receive operation. It takes no
304 arguments, but uses a pkt-line format protocol to communicate with
305 receive-pack to read commands, push-options and send results. In the
306 following example for the protocol, the letter S stands for
307 receive-pack and the letter H stands for this hook.
308
309 # Version and features negotiation.
310 S: PKT-LINE(version=1\0push-options atomic...)
311 S: flush-pkt
312 H: PKT-LINE(version=1\0push-options...)
313 H: flush-pkt
314
315 # Send commands from server to the hook.
316 S: PKT-LINE(<old-oid> <new-oid> <ref>)
317 S: ... ...
318 S: flush-pkt
319 # Send push-options only if the 'push-options' feature is enabled.
320 S: PKT-LINE(push-option)
321 S: ... ...
322 S: flush-pkt
323
324 # Receive result from the hook.
325 # OK, run this command successfully.
326 H: PKT-LINE(ok <ref>)
327 # NO, I reject it.
328 H: PKT-LINE(ng <ref> <reason>)
329 # Fall through, let 'receive-pack' to execute it.
330 H: PKT-LINE(ok <ref>)
331 H: PKT-LINE(option fall-through)
332 # OK, but has an alternate reference. The alternate reference name
333 # and other status can be given in option directives.
334 H: PKT-LINE(ok <ref>)
335 H: PKT-LINE(option refname <refname>)
336 H: PKT-LINE(option old-oid <old-oid>)
337 H: PKT-LINE(option new-oid <new-oid>)
338 H: PKT-LINE(option forced-update)
339 H: ... ...
340 H: flush-pkt
341
342 Each command for the proc-receive hook may point to a pseudo-reference
343 and always has a zero-old as its old-oid, while the proc-receive hook
344 may update an alternate reference and the alternate reference may exist
345 already with a non-zero old-oid. For this case, this hook will use
346 "option" directives to report extended attributes for the reference
347 given by the leading "ok" directive.
348
349 The report of the commands of this hook should have the same order as
350 the input. The exit status of the proc-receive hook only determines the
351 success or failure of the group of commands sent to it, unless atomic
352 push is in use.
353
354 post-receive
355 This hook is invoked by git-receive-pack(1) when it reacts to git push
356 and updates reference(s) in its repository. It executes on the remote
357 repository once after all the refs have been updated.
358
359 This hook executes once for the receive operation. It takes no
360 arguments, but gets the same information as the pre-receive hook does
361 on its standard input.
362
363 This hook does not affect the outcome of git receive-pack, as it is
364 called after the real work is done.
365
366 This supersedes the post-update hook in that it gets both old and new
367 values of all the refs in addition to their names.
368
369 Both standard output and standard error output are forwarded to git
370 send-pack on the other end, so you can simply echo messages for the
371 user.
372
373 The default post-receive hook is empty, but there is a sample script
374 post-receive-email provided in the contrib/hooks directory in Git
375 distribution, which implements sending commit emails.
376
377 The number of push options given on the command line of git push
378 --push-option=... can be read from the environment variable
379 GIT_PUSH_OPTION_COUNT, and the options themselves are found in
380 GIT_PUSH_OPTION_0, GIT_PUSH_OPTION_1,... If it is negotiated to not use
381 the push options phase, the environment variables will not be set. If
382 the client selects to use push options, but doesn’t transmit any, the
383 count variable will be set to zero, GIT_PUSH_OPTION_COUNT=0.
384
385 post-update
386 This hook is invoked by git-receive-pack(1) when it reacts to git push
387 and updates reference(s) in its repository. It executes on the remote
388 repository once after all the refs have been updated.
389
390 It takes a variable number of parameters, each of which is the name of
391 ref that was actually updated.
392
393 This hook is meant primarily for notification, and cannot affect the
394 outcome of git receive-pack.
395
396 The post-update hook can tell what are the heads that were pushed, but
397 it does not know what their original and updated values are, so it is a
398 poor place to do log old..new. The post-receive hook does get both
399 original and updated values of the refs. You might consider it instead
400 if you need them.
401
402 When enabled, the default post-update hook runs git update-server-info
403 to keep the information used by dumb transports (e.g., HTTP) up to
404 date. If you are publishing a Git repository that is accessible via
405 HTTP, you should probably enable this hook.
406
407 Both standard output and standard error output are forwarded to git
408 send-pack on the other end, so you can simply echo messages for the
409 user.
410
411 reference-transaction
412 This hook is invoked by any Git command that performs reference
413 updates. It executes whenever a reference transaction is prepared,
414 committed or aborted and may thus get called multiple times.
415
416 The hook takes exactly one argument, which is the current state the
417 given reference transaction is in:
418
419 • "prepared": All reference updates have been queued to the
420 transaction and references were locked on disk.
421
422 • "committed": The reference transaction was committed and all
423 references now have their respective new value.
424
425 • "aborted": The reference transaction was aborted, no changes were
426 performed and the locks have been released.
427
428 For each reference update that was added to the transaction, the hook
429 receives on standard input a line of the format:
430
431 <old-value> SP <new-value> SP <ref-name> LF
432
433 The exit status of the hook is ignored for any state except for the
434 "prepared" state. In the "prepared" state, a non-zero exit status will
435 cause the transaction to be aborted. The hook will not be called with
436 "aborted" state in that case.
437
438 push-to-checkout
439 This hook is invoked by git-receive-pack(1) when it reacts to git push
440 and updates reference(s) in its repository, and when the push tries to
441 update the branch that is currently checked out and the
442 receive.denyCurrentBranch configuration variable is set to
443 updateInstead. Such a push by default is refused if the working tree
444 and the index of the remote repository has any difference from the
445 currently checked out commit; when both the working tree and the index
446 match the current commit, they are updated to match the newly pushed
447 tip of the branch. This hook is to be used to override the default
448 behaviour.
449
450 The hook receives the commit with which the tip of the current branch
451 is going to be updated. It can exit with a non-zero status to refuse
452 the push (when it does so, it must not modify the index or the working
453 tree). Or it can make any necessary changes to the working tree and to
454 the index to bring them to the desired state when the tip of the
455 current branch is updated to the new commit, and exit with a zero
456 status.
457
458 For example, the hook can simply run git read-tree -u -m HEAD "$1" in
459 order to emulate git fetch that is run in the reverse direction with
460 git push, as the two-tree form of git read-tree -u -m is essentially
461 the same as git switch or git checkout that switches branches while
462 keeping the local changes in the working tree that do not interfere
463 with the difference between the branches.
464
465 pre-auto-gc
466 This hook is invoked by git gc --auto (see git-gc(1)). It takes no
467 parameter, and exiting with non-zero status from this script causes the
468 git gc --auto to abort.
469
470 post-rewrite
471 This hook is invoked by commands that rewrite commits (git-commit(1)
472 when called with --amend and git-rebase(1); however, full-history
473 (re)writing tools like git-fast-import(1) or git-filter-repo[1]
474 typically do not call it!). Its first argument denotes the command it
475 was invoked by: currently one of amend or rebase. Further
476 command-dependent arguments may be passed in the future.
477
478 The hook receives a list of the rewritten commits on stdin, in the
479 format
480
481 <old-sha1> SP <new-sha1> [ SP <extra-info> ] LF
482
483 The extra-info is again command-dependent. If it is empty, the
484 preceding SP is also omitted. Currently, no commands pass any
485 extra-info.
486
487 The hook always runs after the automatic note copying (see
488 "notes.rewrite.<command>" in git-config(1)) has happened, and thus has
489 access to these notes.
490
491 The following command-specific comments apply:
492
493 rebase
494 For the squash and fixup operation, all commits that were squashed
495 are listed as being rewritten to the squashed commit. This means
496 that there will be several lines sharing the same new-sha1.
497
498 The commits are guaranteed to be listed in the order that they were
499 processed by rebase.
500
501 sendemail-validate
502 This hook is invoked by git-send-email(1). It takes a single parameter,
503 the name of the file that holds the e-mail to be sent. Exiting with a
504 non-zero status causes git send-email to abort before sending any
505 e-mails.
506
507 fsmonitor-watchman
508 This hook is invoked when the configuration option core.fsmonitor is
509 set to .git/hooks/fsmonitor-watchman or .git/hooks/fsmonitor-watchmanv2
510 depending on the version of the hook to use.
511
512 Version 1 takes two arguments, a version (1) and the time in elapsed
513 nanoseconds since midnight, January 1, 1970.
514
515 Version 2 takes two arguments, a version (2) and a token that is used
516 for identifying changes since the token. For watchman this would be a
517 clock id. This version must output to stdout the new token followed by
518 a NUL before the list of files.
519
520 The hook should output to stdout the list of all files in the working
521 directory that may have changed since the requested time. The logic
522 should be inclusive so that it does not miss any potential changes. The
523 paths should be relative to the root of the working directory and be
524 separated by a single NUL.
525
526 It is OK to include files which have not actually changed. All changes
527 including newly-created and deleted files should be included. When
528 files are renamed, both the old and the new name should be included.
529
530 Git will limit what files it checks for changes as well as which
531 directories are checked for untracked files based on the path names
532 given.
533
534 An optimized way to tell git "all files have changed" is to return the
535 filename /.
536
537 The exit status determines whether git will use the data from the hook
538 to limit its search. On error, it will fall back to verifying all files
539 and folders.
540
541 p4-changelist
542 This hook is invoked by git-p4 submit.
543
544 The p4-changelist hook is executed after the changelist message has
545 been edited by the user. It can be bypassed with the --no-verify
546 option. It takes a single parameter, the name of the file that holds
547 the proposed changelist text. Exiting with a non-zero status causes the
548 command to abort.
549
550 The hook is allowed to edit the changelist file and can be used to
551 normalize the text into some project standard format. It can also be
552 used to refuse the Submit after inspect the message file.
553
554 Run git-p4 submit --help for details.
555
556 p4-prepare-changelist
557 This hook is invoked by git-p4 submit.
558
559 The p4-prepare-changelist hook is executed right after preparing the
560 default changelist message and before the editor is started. It takes
561 one parameter, the name of the file that contains the changelist text.
562 Exiting with a non-zero status from the script will abort the process.
563
564 The purpose of the hook is to edit the message file in place, and it is
565 not suppressed by the --no-verify option. This hook is called even if
566 --prepare-p4-only is set.
567
568 Run git-p4 submit --help for details.
569
570 p4-post-changelist
571 This hook is invoked by git-p4 submit.
572
573 The p4-post-changelist hook is invoked after the submit has
574 successfully occurred in P4. It takes no parameters and is meant
575 primarily for notification and cannot affect the outcome of the git p4
576 submit action.
577
578 Run git-p4 submit --help for details.
579
580 p4-pre-submit
581 This hook is invoked by git-p4 submit. It takes no parameters and
582 nothing from standard input. Exiting with non-zero status from this
583 script prevent git-p4 submit from launching. It can be bypassed with
584 the --no-verify command line option. Run git-p4 submit --help for
585 details.
586
587 post-index-change
588 This hook is invoked when the index is written in read-cache.c
589 do_write_locked_index.
590
591 The first parameter passed to the hook is the indicator for the working
592 directory being updated. "1" meaning working directory was updated or
593 "0" when the working directory was not updated.
594
595 The second parameter passed to the hook is the indicator for whether or
596 not the index was updated and the skip-worktree bit could have changed.
597 "1" meaning skip-worktree bits could have been updated and "0" meaning
598 they were not.
599
600 Only one parameter should be set to "1" when the hook runs. The hook
601 running passing "1", "1" should not be possible.
602
604 Part of the git(1) suite
605
607 1. git-filter-repo
608 https://github.com/newren/git-filter-repo
609
610
611
612Git 2.31.1 2021-03-26 GITHOOKS(5)