1Hooks used by git 2================= 3 4Hooks are little scripts you can place in `$GIT_DIR/hooks` 5directory to trigger action at certain points. When 6`git-init` is run, a handful example hooks are copied in the 7`hooks` directory of the new repository, but by default they are 8all disabled. To enable a hook, make it executable with `chmod +x`. 9 10This document describes the currently defined hooks. 11 12applypatch-msg 13-------------- 14 15This hook is invoked by `git-am` script. It takes a single 16parameter, the name of the file that holds the proposed commit 17log message. Exiting with non-zero status causes 18`git-am` to abort before applying the patch. 19 20The hook is allowed to edit the message file in place, and can 21be used to normalize the message into some project standard 22format (if the project has one). It can also be used to refuse 23the commit after inspecting the message file. 24 25The default 'applypatch-msg' hook, when enabled, runs the 26'commit-msg' hook, if the latter is enabled. 27 28pre-applypatch 29-------------- 30 31This hook is invoked by `git-am`. It takes no parameter, and is 32invoked after the patch is applied, but before a commit is made. 33 34If it exits with non-zero status, then the working tree will not be 35committed after applying the patch. 36 37It can be used to inspect the current working tree and refuse to 38make a commit if it does not pass certain test. 39 40The default 'pre-applypatch' hook, when enabled, runs the 41'pre-commit' hook, if the latter is enabled. 42 43post-applypatch 44--------------- 45 46This hook is invoked by `git-am`. It takes no parameter, 47and is invoked after the patch is applied and a commit is made. 48 49This hook is meant primarily for notification, and cannot affect 50the outcome of `git-am`. 51 52pre-commit 53---------- 54 55This hook is invoked by `git-commit`, and can be bypassed 56with `\--no-verify` option. It takes no parameter, and is 57invoked before obtaining the proposed commit log message and 58making a commit. Exiting with non-zero status from this script 59causes the `git-commit` to abort. 60 61The default 'pre-commit' hook, when enabled, catches introduction 62of lines with trailing whitespaces and aborts the commit when 63such a line is found. 64 65All the `git-commit` hooks are invoked with the environment 66variable `GIT_EDITOR=:` if the command will not bring up an editor 67to modify the commit message. 68 69prepare-commit-msg 70------------------ 71 72This hook is invoked by `git-commit` right after preparing the 73default log message, and before the editor is started. 74 75It takes one to three parameters. The first is the name of the file 76that the commit log message. The second is the source of the commit 77message, and can be: `message` (if a `\-m` or `\-F` option was 78given); `template` (if a `\-t` option was given or the 79configuration option `commit.template` is set); `merge` (if the 80commit is a merge or a `.git/MERGE_MSG` file exists); `squash` 81(if a `.git/SQUASH_MSG` file exists); or `commit`, followed by 82a commit SHA1 (if a `\-c`, `\-C` or `\--amend` option was given). 83 84If the exit status is non-zero, `git-commit` will abort. 85 86The purpose of the hook is to edit the message file in place, and 87it is not suppressed by the `\--no-verify` option. A non-zero exit 88means a failure of the hook and aborts the commit. It should not 89be used as replacement for pre-commit hook. 90 91The sample `prepare-commit-msg` hook that comes with git comments 92out the `Conflicts:` part of a merge's commit message. 93 94commit-msg 95---------- 96 97This hook is invoked by `git-commit`, and can be bypassed 98with `\--no-verify` option. It takes a single parameter, the 99name of the file that holds the proposed commit log message. 100Exiting with non-zero status causes the `git-commit` to 101abort. 102 103The hook is allowed to edit the message file in place, and can 104be used to normalize the message into some project standard 105format (if the project has one). It can also be used to refuse 106the commit after inspecting the message file. 107 108The default 'commit-msg' hook, when enabled, detects duplicate 109"Signed-off-by" lines, and aborts the commit if one is found. 110 111post-commit 112----------- 113 114This hook is invoked by `git-commit`. It takes no 115parameter, and is invoked after a commit is made. 116 117This hook is meant primarily for notification, and cannot affect 118the outcome of `git-commit`. 119 120post-checkout 121----------- 122 123This hook is invoked when a `git-checkout` is run after having updated the 124worktree. The hook is given three parameters: the ref of the previous HEAD, 125the ref of the new HEAD (which may or may not have changed), and a flag 126indicating whether the checkout was a branch checkout (changing branches, 127flag=1) or a file checkout (retrieving a file from the index, flag=0). 128This hook cannot affect the outcome of `git-checkout`. 129 130This hook can be used to perform repository validity checks, auto-display 131differences from the previous HEAD if different, or set working dir metadata 132properties. 133 134post-merge 135----------- 136 137This hook is invoked by `git-merge`, which happens when a `git pull` 138is done on a local repository. The hook takes a single parameter, a status 139flag specifying whether or not the merge being done was a squash merge. 140This hook cannot affect the outcome of `git-merge`. 141 142This hook can be used in conjunction with a corresponding pre-commit hook to 143save and restore any form of metadata associated with the working tree 144(eg: permissions/ownership, ACLS, etc). See contrib/hooks/setgitperms.perl 145for an example of how to do this. 146 147[[pre-receive]] 148pre-receive 149----------- 150 151This hook is invoked by `git-receive-pack` on the remote repository, 152which happens when a `git push` is done on a local repository. 153Just before starting to update refs on the remote repository, the 154pre-receive hook is invoked. Its exit status determines the success 155or failure of the update. 156 157This hook executes once for the receive operation. It takes no 158arguments, but for each ref to be updated it receives on standard 159input a line of the format: 160 161 <old-value> SP <new-value> SP <ref-name> LF 162 163where `<old-value>` is the old object name stored in the ref, 164`<new-value>` is the new object name to be stored in the ref and 165`<ref-name>` is the full name of the ref. 166When creating a new ref, `<old-value>` is 40 `0`. 167 168If the hook exits with non-zero status, none of the refs will be 169updated. If the hook exits with zero, updating of individual refs can 170still be prevented by the <<update,'update'>> hook. 171 172Both standard output and standard error output are forwarded to 173`git-send-pack` on the other end, so you can simply `echo` messages 174for the user. 175 176[[update]] 177update 178------ 179 180This hook is invoked by `git-receive-pack` on the remote repository, 181which happens when a `git push` is done on a local repository. 182Just before updating the ref on the remote repository, the update hook 183is invoked. Its exit status determines the success or failure of 184the ref update. 185 186The hook executes once for each ref to be updated, and takes 187three parameters: 188 189 - the name of the ref being updated, 190 - the old object name stored in the ref, 191 - and the new objectname to be stored in the ref. 192 193A zero exit from the update hook allows the ref to be updated. 194Exiting with a non-zero status prevents `git-receive-pack` 195from updating that ref. 196 197This hook can be used to prevent 'forced' update on certain refs by 198making sure that the object name is a commit object that is a 199descendant of the commit object named by the old object name. 200That is, to enforce a "fast forward only" policy. 201 202It could also be used to log the old..new status. However, it 203does not know the entire set of branches, so it would end up 204firing one e-mail per ref when used naively, though. The 205<<post-receive,'post-receive'>> hook is more suited to that. 206 207Another use suggested on the mailing list is to use this hook to 208implement access control which is finer grained than the one 209based on filesystem group. 210 211Both standard output and standard error output are forwarded to 212`git-send-pack` on the other end, so you can simply `echo` messages 213for the user. 214 215The default 'update' hook, when enabled--and with 216`hooks.allowunannotated` config option turned on--prevents 217unannotated tags to be pushed. 218 219[[post-receive]] 220post-receive 221------------ 222 223This hook is invoked by `git-receive-pack` on the remote repository, 224which happens when a `git push` is done on a local repository. 225It executes on the remote repository once after all the refs have 226been updated. 227 228This hook executes once for the receive operation. It takes no 229arguments, but gets the same information as the 230<<pre-receive,'pre-receive'>> 231hook does on its standard input. 232 233This hook does not affect the outcome of `git-receive-pack`, as it 234is called after the real work is done. 235 236This supersedes the <<post-update,'post-update'>> hook in that it gets 237both old and new values of all the refs in addition to their 238names. 239 240Both standard output and standard error output are forwarded to 241`git-send-pack` on the other end, so you can simply `echo` messages 242for the user. 243 244The default 'post-receive' hook is empty, but there is 245a sample script `post-receive-email` provided in the `contrib/hooks` 246directory in git distribution, which implements sending commit 247emails. 248 249[[post-update]] 250post-update 251----------- 252 253This hook is invoked by `git-receive-pack` on the remote repository, 254which happens when a `git push` is done on a local repository. 255It executes on the remote repository once after all the refs have 256been updated. 257 258It takes a variable number of parameters, each of which is the 259name of ref that was actually updated. 260 261This hook is meant primarily for notification, and cannot affect 262the outcome of `git-receive-pack`. 263 264The 'post-update' hook can tell what are the heads that were pushed, 265but it does not know what their original and updated values are, 266so it is a poor place to do log old..new. The 267<<post-receive,'post-receive'>> hook does get both original and 268updated values of the refs. You might consider it instead if you need 269them. 270 271When enabled, the default 'post-update' hook runs 272`git-update-server-info` to keep the information used by dumb 273transports (e.g., HTTP) up-to-date. If you are publishing 274a git repository that is accessible via HTTP, you should 275probably enable this hook. 276 277Both standard output and standard error output are forwarded to 278`git-send-pack` on the other end, so you can simply `echo` messages 279for the user. 280 281pre-auto-gc 282----------- 283 284This hook is invoked by `git-gc --auto`. It takes no parameter, and 285exiting with non-zero status from this script causes the `git-gc --auto` 286to abort.