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