Home Explore Blog CI



git

1st chunk of `Documentation/githooks.adoc`
098aafe3679637823e9c29698506b9762b29734ce4c315ad0000000100000fa3
githooks(5)
===========

NAME
----
githooks - Hooks used by Git

SYNOPSIS
--------
$GIT_DIR/hooks/* (or \`git config core.hooksPath`/*)


DESCRIPTION
-----------

Hooks are programs you can place in a hooks directory to trigger
actions at certain points in git's execution. Hooks that don't have
the executable bit set are ignored.

By default the hooks directory is `$GIT_DIR/hooks`, but that can be
changed via the `core.hooksPath` configuration variable (see
linkgit:git-config[1]).

Before Git invokes a hook, it changes its working directory to either
$GIT_DIR in a bare repository or the root of the working tree in a non-bare
repository. An exception are hooks triggered during a push ('pre-receive',
'update', 'post-receive', 'post-update', 'push-to-checkout') which are always
executed in $GIT_DIR.

Environment variables, such as `GIT_DIR`, `GIT_WORK_TREE`, etc., are exported
so that Git commands run by the hook can correctly locate the repository.  If
your hook needs to invoke Git commands in a foreign repository or in a
different working tree of the same repository, then it should clear these
environment variables so they do not interfere with Git operations at the
foreign location.  For example:

------------
local_desc=$(git describe)
foreign_desc=$(unset $(git rev-parse --local-env-vars); git -C ../foreign-repo describe)
------------

Hooks can get their arguments via the environment, command-line
arguments, and stdin. See the documentation for each hook below for
details.

`git init` may copy hooks to the new repository, depending on its
configuration. See the "TEMPLATE DIRECTORY" section in
linkgit:git-init[1] for details. When the rest of this document refers
to "default hooks" it's talking about the default template shipped
with Git.

The currently supported hooks are described below.

HOOKS
-----

applypatch-msg
~~~~~~~~~~~~~~

This hook is invoked by linkgit:git-am[1].  It takes a single
parameter, the name of the file that holds the proposed commit
log message.  Exiting with a non-zero status causes `git am` to abort
before applying the patch.

The hook is allowed to edit the message file in place, and can
be used to normalize the message into some project standard
format. It can also be used to refuse the commit after inspecting
the message file.

The default 'applypatch-msg' hook, when enabled, runs the
'commit-msg' hook, if the latter is enabled.

pre-applypatch
~~~~~~~~~~~~~~

This hook is invoked by linkgit:git-am[1].  It takes no parameter, and is
invoked after the patch is applied, but before a commit is made.

If it exits with non-zero status, then the working tree will not be
committed after applying the patch.

It can be used to inspect the current working tree and refuse to
make a commit if it does not pass certain tests.

The default 'pre-applypatch' hook, when enabled, runs the
'pre-commit' hook, if the latter is enabled.

post-applypatch
~~~~~~~~~~~~~~~

This hook is invoked by linkgit:git-am[1].  It takes no parameter,
and is invoked after the patch is applied and a commit is made.

This hook is meant primarily for notification, and cannot affect
the outcome of `git am`.

pre-commit
~~~~~~~~~~

This hook is invoked by linkgit:git-commit[1], and can be bypassed
with the `--no-verify` option.  It takes no parameters, and is
invoked before obtaining the proposed commit log message and
making a commit.  Exiting with a non-zero status from this script
causes the `git commit` command to abort before creating a commit.

The default 'pre-commit' hook, when enabled, catches introduction
of lines with trailing whitespaces and aborts the commit when
such a line is found.

All the `git commit` hooks are invoked with the environment
variable `GIT_EDITOR=:` if the command will not bring up an editor
to modify the commit message.

The default 'pre-commit' hook, when enabled--and with the
`hooks.allownonascii` config option unset or set to false--prevents
the use of non-ASCII filenames.

pre-merge-commit
~~~~~~~~~~~~~~~~

Title: Git Hooks
Summary
Git hooks are programs that can be used to trigger actions at certain points in Git's execution, such as before or after a commit, and can be used to enforce project standards, run tests, or perform other automated tasks to ensure the integrity and quality of a repository. Hooks can be placed in a hooks directory and are executed by Git at specific points, passing arguments via the environment, command-line, and stdin. Supported hooks include applypatch-msg, pre-applypatch, post-applypatch, pre-commit, and pre-merge-commit, among others, each serving a unique purpose in the Git workflow.