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