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