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