githooks.txt: improve the intro section
[git.git] / Documentation / githooks.txt
blob0d94337a5b7162c1827ab0627a7f8f0042e126a2
1 githooks(5)
2 ===========
4 NAME
5 ----
6 githooks - Hooks used by Git
8 SYNOPSIS
9 --------
10 $GIT_DIR/hooks/*
13 DESCRIPTION
14 -----------
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.
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.
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.
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.
34 The currently supported hooks are described below.
36 HOOKS
37 -----
39 applypatch-msg
40 ~~~~~~~~~~~~~~
42 This hook is invoked by 'git am' script.  It takes a single
43 parameter, the name of the file that holds the proposed commit
44 log message.  Exiting with non-zero status causes
45 'git am' to abort before applying the patch.
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
49 format (if the project has one). It can also be used to refuse
50 the commit after inspecting the message file.
52 The default 'applypatch-msg' hook, when enabled, runs the
53 'commit-msg' hook, if the latter is enabled.
55 pre-applypatch
56 ~~~~~~~~~~~~~~
58 This hook is invoked by 'git am'.  It takes no parameter, and is
59 invoked after the patch is applied, but before a commit is made.
61 If it exits with non-zero status, then the working tree will not be
62 committed after applying the patch.
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.
67 The default 'pre-applypatch' hook, when enabled, runs the
68 'pre-commit' hook, if the latter is enabled.
70 post-applypatch
71 ~~~~~~~~~~~~~~~
73 This hook is invoked by 'git am'.  It takes no parameter,
74 and is invoked after the patch is applied and a commit is made.
76 This hook is meant primarily for notification, and cannot affect
77 the outcome of 'git am'.
79 pre-commit
80 ~~~~~~~~~~
82 This hook is invoked by 'git commit', and can be bypassed
83 with `--no-verify` option.  It takes no parameter, and is
84 invoked before obtaining the proposed commit log message and
85 making a commit.  Exiting with non-zero status from this script
86 causes the 'git commit' to abort.
88 The default 'pre-commit' hook, when enabled, catches introduction
89 of lines with trailing whitespaces and aborts the commit when
90 such a line is found.
92 All the 'git commit' hooks are invoked with the environment
93 variable `GIT_EDITOR=:` if the command will not bring up an editor
94 to modify the commit message.
96 prepare-commit-msg
97 ~~~~~~~~~~~~~~~~~~
99 This hook is invoked by 'git commit' right after preparing the
100 default log message, and before the editor is started.
102 It takes one to three parameters.  The first is the name of the file
103 that contains the commit log message.  The second is the source of the commit
104 message, and can be: `message` (if a `-m` or `-F` option was
105 given); `template` (if a `-t` option was given or the
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
109 a commit SHA-1 (if a `-c`, `-C` or `--amend` option was given).
111 If the exit status is non-zero, 'git commit' will abort.
113 The purpose of the hook is to edit the message file in place, and
114 it is not suppressed by the `--no-verify` option.  A non-zero exit
115 means a failure of the hook and aborts the commit.  It should not
116 be used as replacement for pre-commit hook.
118 The sample `prepare-commit-msg` hook that comes with Git comments
119 out the `Conflicts:` part of a merge's commit message.
121 commit-msg
122 ~~~~~~~~~~
124 This hook is invoked by 'git commit', and can be bypassed
125 with `--no-verify` option.  It takes a single parameter, the
126 name of the file that holds the proposed commit log message.
127 Exiting with non-zero status causes the 'git commit' to
128 abort.
130 The hook is allowed to edit the message file in place, and can
131 be used to normalize the message into some project standard
132 format (if the project has one). It can also be used to refuse
133 the commit after inspecting the message file.
135 The default 'commit-msg' hook, when enabled, detects duplicate
136 "Signed-off-by" lines, and aborts the commit if one is found.
138 post-commit
139 ~~~~~~~~~~~
141 This hook is invoked by 'git commit'.  It takes no
142 parameter, and is invoked after a commit is made.
144 This hook is meant primarily for notification, and cannot affect
145 the outcome of 'git commit'.
147 pre-rebase
148 ~~~~~~~~~~
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.
156 post-checkout
157 ~~~~~~~~~~~~~
159 This hook is invoked when a 'git checkout' is run after having updated the
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).
164 This hook cannot affect the outcome of 'git checkout'.
166 It is also run after 'git clone', unless the --no-checkout (-n) option is
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.
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.
174 post-merge
175 ~~~~~~~~~~
177 This hook is invoked by 'git merge', which happens when a 'git pull'
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.
180 This hook cannot affect the outcome of 'git merge' and is not executed,
181 if the merge failed due to conflicts.
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
185 (e.g.: permissions/ownership, ACLS, etc).  See contrib/hooks/setgitperms.perl
186 for an example of how to do this.
188 pre-push
189 ~~~~~~~~
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.
196 Information about what is to be pushed is provided on the hook's standard
197 input with lines of the form:
199   <local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF
201 For instance, if the command +git push origin master:foreign+ were run the
202 hook would receive a line like the following:
204   refs/heads/master 67890 refs/heads/foreign 12345
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
208 deleted, the `<local ref>` will be supplied as `(delete)` and the `<local
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
211 supplied as it was originally given.
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.
217 [[pre-receive]]
218 pre-receive
219 ~~~~~~~~~~~
221 This hook is invoked by 'git-receive-pack' on the remote repository,
222 which happens when a 'git push' is done on a local repository.
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.
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:
231   <old-value> SP <new-value> SP <ref-name> LF
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`.
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.
242 Both standard output and standard error output are forwarded to
243 'git send-pack' on the other end, so you can simply `echo` messages
244 for the user.
246 [[update]]
247 update
248 ~~~~~~
250 This hook is invoked by 'git-receive-pack' on the remote repository,
251 which happens when a 'git push' is done on a local repository.
252 Just before updating the ref on the remote repository, the update hook
253 is invoked.  Its exit status determines the success or failure of
254 the ref update.
256 The hook executes once for each ref to be updated, and takes
257 three parameters:
259  - the name of the ref being updated,
260  - the old object name stored in the ref,
261  - and the new object name to be stored in the ref.
263 A zero exit from the update hook allows the ref to be updated.
264 Exiting with a non-zero status prevents 'git-receive-pack'
265 from updating that ref.
267 This hook can be used to prevent 'forced' update on certain refs by
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.
270 That is, to enforce a "fast-forward only" policy.
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
274 firing one e-mail per ref when used naively, though.  The
275 <<post-receive,'post-receive'>> hook is more suited to that.
277 Another use suggested on the mailing list is to use this hook to
278 implement access control which is finer grained than the one
279 based on filesystem group.
281 Both standard output and standard error output are forwarded to
282 'git send-pack' on the other end, so you can simply `echo` messages
283 for the user.
285 The default 'update' hook, when enabled--and with
286 `hooks.allowunannotated` config option unset or set to false--prevents
287 unannotated tags to be pushed.
289 [[post-receive]]
290 post-receive
291 ~~~~~~~~~~~~
293 This hook is invoked by 'git-receive-pack' on the remote repository,
294 which happens when a 'git push' is done on a local repository.
295 It executes on the remote repository once after all the refs have
296 been updated.
298 This hook executes once for the receive operation.  It takes no
299 arguments, but gets the same information as the
300 <<pre-receive,'pre-receive'>>
301 hook does on its standard input.
303 This hook does not affect the outcome of 'git-receive-pack', as it
304 is called after the real work is done.
306 This supersedes the <<post-update,'post-update'>> hook in that it gets
307 both old and new values of all the refs in addition to their
308 names.
310 Both standard output and standard error output are forwarded to
311 'git send-pack' on the other end, so you can simply `echo` messages
312 for the user.
314 The default 'post-receive' hook is empty, but there is
315 a sample script `post-receive-email` provided in the `contrib/hooks`
316 directory in Git distribution, which implements sending commit
317 emails.
319 [[post-update]]
320 post-update
321 ~~~~~~~~~~~
323 This hook is invoked by 'git-receive-pack' on the remote repository,
324 which happens when a 'git push' is done on a local repository.
325 It executes on the remote repository once after all the refs have
326 been updated.
328 It takes a variable number of parameters, each of which is the
329 name of ref that was actually updated.
331 This hook is meant primarily for notification, and cannot affect
332 the outcome of 'git-receive-pack'.
334 The 'post-update' hook can tell what are the heads that were pushed,
335 but it does not know what their original and updated values are,
336 so it is a poor place to do log old..new. The
337 <<post-receive,'post-receive'>> hook does get both original and
338 updated values of the refs. You might consider it instead if you need
339 them.
341 When enabled, the default 'post-update' hook runs
342 'git update-server-info' to keep the information used by dumb
343 transports (e.g., HTTP) up-to-date.  If you are publishing
344 a Git repository that is accessible via HTTP, you should
345 probably enable this hook.
347 Both standard output and standard error output are forwarded to
348 'git send-pack' on the other end, so you can simply `echo` messages
349 for the user.
351 push-to-checkout
352 ~~~~~~~~~~~~~~~~
354 This hook is invoked by 'git-receive-pack' on the remote repository,
355 which happens when a 'git push' is done on a local repository, when
356 the push tries to update the branch that is currently checked out
357 and the `receive.denyCurrentBranch` configuration variable is set to
358 `updateInstead`.  Such a push by default is refused if the working
359 tree and the index of the remote repository has any difference from
360 the currently checked out commit; when both the working tree and the
361 index match the current commit, they are updated to match the newly
362 pushed tip of the branch.  This hook is to be used to override the
363 default behaviour.
365 The hook receives the commit with which the tip of the current
366 branch is going to be updated.  It can exit with a non-zero status
367 to refuse the push (when it does so, it must not modify the index or
368 the working tree).  Or it can make any necessary changes to the
369 working tree and to the index to bring them to the desired state
370 when the tip of the current branch is updated to the new commit, and
371 exit with a zero status.
373 For example, the hook can simply run `git read-tree -u -m HEAD "$1"`
374 in order to emulate 'git fetch' that is run in the reverse direction
375 with `git push`, as the two-tree form of `read-tree -u -m` is
376 essentially the same as `git checkout` that switches branches while
377 keeping the local changes in the working tree that do not interfere
378 with the difference between the branches.
381 pre-auto-gc
382 ~~~~~~~~~~~
384 This hook is invoked by 'git gc --auto'. It takes no parameter, and
385 exiting with non-zero status from this script causes the 'git gc --auto'
386 to abort.
388 post-rewrite
389 ~~~~~~~~~~~~
391 This hook is invoked by commands that rewrite commits (`git commit
392 --amend`, 'git-rebase'; currently 'git-filter-branch' does 'not' call
393 it!).  Its first argument denotes the command it was invoked by:
394 currently one of `amend` or `rebase`.  Further command-dependent
395 arguments may be passed in the future.
397 The hook receives a list of the rewritten commits on stdin, in the
398 format
400   <old-sha1> SP <new-sha1> [ SP <extra-info> ] LF
402 The 'extra-info' is again command-dependent.  If it is empty, the
403 preceding SP is also omitted.  Currently, no commands pass any
404 'extra-info'.
406 The hook always runs after the automatic note copying (see
407 "notes.rewrite.<command>" in linkgit:git-config[1]) has happened, and
408 thus has access to these notes.
410 The following command-specific comments apply:
412 rebase::
413         For the 'squash' and 'fixup' operation, all commits that were
414         squashed are listed as being rewritten to the squashed commit.
415         This means that there will be several lines sharing the same
416         'new-sha1'.
418 The commits are guaranteed to be listed in the order that they were
419 processed by rebase.
424 Part of the linkgit:git[1] suite