<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">

<article lang="en" id="githooks(5)">
<articleinfo>
    <title>githooks(5)</title>
        <indexterm>
                <primary>githooks(5)</primary>
        </indexterm>
</articleinfo>
<simplesect id="_name">
<title>NAME</title>
<simpara>githooks - Hooks used by git</simpara>
</simplesect>
<simplesect id="_synopsis">
<title>SYNOPSIS</title>
<simpara>$GIT_DIR/hooks/*</simpara>
</simplesect>
<simplesect id="_description">
<title>DESCRIPTION</title>
<simpara>Hooks are little scripts you can place in <literal>$GIT_DIR/hooks</literal>
directory to trigger action at certain points.  When
<emphasis>git-init</emphasis> is run, a handful of example hooks are copied into the
<literal>hooks</literal> directory of the new repository, but by default they are
all disabled.  To enable a hook, rename it by removing its <literal>.sample</literal>
suffix.</simpara>
<note><simpara>It is also a requirement for a given hook to be executable.
However - in a freshly initialized repository - the <literal>.sample</literal> files are
executable by default.</simpara></note>
<simpara>This document describes the currently defined hooks.</simpara>
</simplesect>
<simplesect id="_applypatch_msg">
<title>applypatch-msg</title>
<simpara>This hook is invoked by <emphasis>git-am</emphasis> script.  It takes a single
parameter, the name of the file that holds the proposed commit
log message.  Exiting with non-zero status causes
<emphasis>git-am</emphasis> to abort before applying the patch.</simpara>
<simpara>The hook is allowed to edit the message file in place, and can
be used to normalize the message into some project standard
format (if the project has one). It can also be used to refuse
the commit after inspecting the message file.</simpara>
<simpara>The default <emphasis>applypatch-msg</emphasis> hook, when enabled, runs the
<emphasis>commit-msg</emphasis> hook, if the latter is enabled.</simpara>
</simplesect>
<simplesect id="_pre_applypatch">
<title>pre-applypatch</title>
<simpara>This hook is invoked by <emphasis>git-am</emphasis>.  It takes no parameter, and is
invoked after the patch is applied, but before a commit is made.</simpara>
<simpara>If it exits with non-zero status, then the working tree will not be
committed after applying the patch.</simpara>
<simpara>It can be used to inspect the current working tree and refuse to
make a commit if it does not pass certain test.</simpara>
<simpara>The default <emphasis>pre-applypatch</emphasis> hook, when enabled, runs the
<emphasis>pre-commit</emphasis> hook, if the latter is enabled.</simpara>
</simplesect>
<simplesect id="_post_applypatch">
<title>post-applypatch</title>
<simpara>This hook is invoked by <emphasis>git-am</emphasis>.  It takes no parameter,
and is invoked after the patch is applied and a commit is made.</simpara>
<simpara>This hook is meant primarily for notification, and cannot affect
the outcome of <emphasis>git-am</emphasis>.</simpara>
</simplesect>
<simplesect id="_pre_commit">
<title>pre-commit</title>
<simpara>This hook is invoked by <emphasis>git-commit</emphasis>, and can be bypassed
with <literal>--no-verify</literal> option.  It takes no parameter, and is
invoked before obtaining the proposed commit log message and
making a commit.  Exiting with non-zero status from this script
causes the <emphasis>git-commit</emphasis> to abort.</simpara>
<simpara>The default <emphasis>pre-commit</emphasis> hook, when enabled, catches introduction
of lines with trailing whitespaces and aborts the commit when
such a line is found.</simpara>
<simpara>All the <emphasis>git-commit</emphasis> hooks are invoked with the environment
variable <literal>GIT_EDITOR=:</literal> if the command will not bring up an editor
to modify the commit message.</simpara>
</simplesect>
<simplesect id="_prepare_commit_msg">
<title>prepare-commit-msg</title>
<simpara>This hook is invoked by <emphasis>git-commit</emphasis> right after preparing the
default log message, and before the editor is started.</simpara>
<simpara>It takes one to three parameters.  The first is the name of the file
that contains the commit log message.  The second is the source of the commit
message, and can be: <literal>message</literal> (if a <literal>-m</literal> or <literal>-F</literal> option was
given); <literal>template</literal> (if a <literal>-t</literal> option was given or the
configuration option <literal>commit.template</literal> is set); <literal>merge</literal> (if the
commit is a merge or a <literal>.git/MERGE_MSG</literal> file exists); <literal>squash</literal>
(if a <literal>.git/SQUASH_MSG</literal> file exists); or <literal>commit</literal>, followed by
a commit SHA1 (if a <literal>-c</literal>, <literal>-C</literal> or <literal>--amend</literal> option was given).</simpara>
<simpara>If the exit status is non-zero, <emphasis>git-commit</emphasis> will abort.</simpara>
<simpara>The purpose of the hook is to edit the message file in place, and
it is not suppressed by the <literal>--no-verify</literal> option.  A non-zero exit
means a failure of the hook and aborts the commit.  It should not
be used as replacement for pre-commit hook.</simpara>
<simpara>The sample <literal>prepare-commit-msg</literal> hook that comes with git comments
out the <literal>Conflicts:</literal> part of a merge&#8217;s commit message.</simpara>
</simplesect>
<simplesect id="_commit_msg">
<title>commit-msg</title>
<simpara>This hook is invoked by <emphasis>git-commit</emphasis>, and can be bypassed
with <literal>--no-verify</literal> option.  It takes a single parameter, the
name of the file that holds the proposed commit log message.
Exiting with non-zero status causes the <emphasis>git-commit</emphasis> to
abort.</simpara>
<simpara>The hook is allowed to edit the message file in place, and can
be used to normalize the message into some project standard
format (if the project has one). It can also be used to refuse
the commit after inspecting the message file.</simpara>
<simpara>The default <emphasis>commit-msg</emphasis> hook, when enabled, detects duplicate
"Signed-off-by" lines, and aborts the commit if one is found.</simpara>
</simplesect>
<simplesect id="_post_commit">
<title>post-commit</title>
<simpara>This hook is invoked by <emphasis>git-commit</emphasis>.  It takes no
parameter, and is invoked after a commit is made.</simpara>
<simpara>This hook is meant primarily for notification, and cannot affect
the outcome of <emphasis>git-commit</emphasis>.</simpara>
</simplesect>
<simplesect id="_pre_rebase">
<title>pre-rebase</title>
<simpara>This hook is called by <emphasis>git-rebase</emphasis> and can be used to prevent a branch
from getting rebased.</simpara>
</simplesect>
<simplesect id="_post_checkout">
<title>post-checkout</title>
<simpara>This hook is invoked when a <emphasis>git-checkout</emphasis> is run after having updated the
worktree.  The hook is given three parameters: the ref of the previous HEAD,
the ref of the new HEAD (which may or may not have changed), and a flag
indicating whether the checkout was a branch checkout (changing branches,
flag=1) or a file checkout (retrieving a file from the index, flag=0).
This hook cannot affect the outcome of <emphasis>git-checkout</emphasis>.</simpara>
<simpara>This hook can be used to perform repository validity checks, auto-display
differences from the previous HEAD if different, or set working dir metadata
properties.</simpara>
</simplesect>
<simplesect id="_post_merge">
<title>post-merge</title>
<simpara>This hook is invoked by <emphasis>git-merge</emphasis>, which happens when a <emphasis>git-pull</emphasis>
is done on a local repository.  The hook takes a single parameter, a status
flag specifying whether or not the merge being done was a squash merge.
This hook cannot affect the outcome of <emphasis>git-merge</emphasis> and is not executed,
if the merge failed due to conflicts.</simpara>
<simpara>This hook can be used in conjunction with a corresponding pre-commit hook to
save and restore any form of metadata associated with the working tree
(eg: permissions/ownership, ACLS, etc).  See contrib/hooks/setgitperms.perl
for an example of how to do this.</simpara>
</simplesect>
<simplesect id="pre-receive">
<title>pre-receive</title>
<simpara>This hook is invoked by <emphasis>git-receive-pack</emphasis> on the remote repository,
which happens when a <emphasis>git-push</emphasis> is done on a local repository.
Just before starting to update refs on the remote repository, the
pre-receive hook is invoked.  Its exit status determines the success
or failure of the update.</simpara>
<simpara>This hook executes once for the receive operation. It takes no
arguments, but for each ref to be updated it receives on standard
input a line of the format:</simpara>
<literallayout class="monospaced">&lt;old-value&gt; SP &lt;new-value&gt; SP &lt;ref-name&gt; LF</literallayout>
<simpara>where <literal>&lt;old-value&gt;</literal> is the old object name stored in the ref,
<literal>&lt;new-value&gt;</literal> is the new object name to be stored in the ref and
<literal>&lt;ref-name&gt;</literal> is the full name of the ref.
When creating a new ref, <literal>&lt;old-value&gt;</literal> is 40 <literal>0</literal>.</simpara>
<simpara>If the hook exits with non-zero status, none of the refs will be
updated. If the hook exits with zero, updating of individual refs can
still be prevented by the <link linkend="update"><emphasis>update</emphasis></link> hook.</simpara>
<simpara>Both standard output and standard error output are forwarded to
<emphasis>git-send-pack</emphasis> on the other end, so you can simply <literal>echo</literal> messages
for the user.</simpara>
</simplesect>
<simplesect id="update">
<title>update</title>
<simpara>This hook is invoked by <emphasis>git-receive-pack</emphasis> on the remote repository,
which happens when a <emphasis>git-push</emphasis> is done on a local repository.
Just before updating the ref on the remote repository, the update hook
is invoked.  Its exit status determines the success or failure of
the ref update.</simpara>
<simpara>The hook executes once for each ref to be updated, and takes
three parameters:</simpara>
<itemizedlist>
<listitem>
<simpara>
the name of the ref being updated,
</simpara>
</listitem>
<listitem>
<simpara>
the old object name stored in the ref,
</simpara>
</listitem>
<listitem>
<simpara>
and the new objectname to be stored in the ref.
</simpara>
</listitem>
</itemizedlist>
<simpara>A zero exit from the update hook allows the ref to be updated.
Exiting with a non-zero status prevents <emphasis>git-receive-pack</emphasis>
from updating that ref.</simpara>
<simpara>This hook can be used to prevent <emphasis>forced</emphasis> update on certain refs by
making sure that the object name is a commit object that is a
descendant of the commit object named by the old object name.
That is, to enforce a "fast forward only" policy.</simpara>
<simpara>It could also be used to log the old..new status.  However, it
does not know the entire set of branches, so it would end up
firing one e-mail per ref when used naively, though.  The
<link linkend="post-receive"><emphasis>post-receive</emphasis></link> hook is more suited to that.</simpara>
<simpara>Another use suggested on the mailing list is to use this hook to
implement access control which is finer grained than the one
based on filesystem group.</simpara>
<simpara>Both standard output and standard error output are forwarded to
<emphasis>git-send-pack</emphasis> on the other end, so you can simply <literal>echo</literal> messages
for the user.</simpara>
<simpara>The default <emphasis>update</emphasis> hook, when enabled&#8212;and with
<literal>hooks.allowunannotated</literal> config option turned on&#8212;prevents
unannotated tags to be pushed.</simpara>
</simplesect>
<simplesect id="post-receive">
<title>post-receive</title>
<simpara>This hook is invoked by <emphasis>git-receive-pack</emphasis> on the remote repository,
which happens when a <emphasis>git-push</emphasis> is done on a local repository.
It executes on the remote repository once after all the refs have
been updated.</simpara>
<simpara>This hook executes once for the receive operation.  It takes no
arguments, but gets the same information as the
<link linkend="pre-receive"><emphasis>pre-receive</emphasis></link>
hook does on its standard input.</simpara>
<simpara>This hook does not affect the outcome of <emphasis>git-receive-pack</emphasis>, as it
is called after the real work is done.</simpara>
<simpara>This supersedes the <link linkend="post-update"><emphasis>post-update</emphasis></link> hook in that it gets
both old and new values of all the refs in addition to their
names.</simpara>
<simpara>Both standard output and standard error output are forwarded to
<emphasis>git-send-pack</emphasis> on the other end, so you can simply <literal>echo</literal> messages
for the user.</simpara>
<simpara>The default <emphasis>post-receive</emphasis> hook is empty, but there is
a sample script <literal>post-receive-email</literal> provided in the <literal>contrib/hooks</literal>
directory in git distribution, which implements sending commit
emails.</simpara>
</simplesect>
<simplesect id="post-update">
<title>post-update</title>
<simpara>This hook is invoked by <emphasis>git-receive-pack</emphasis> on the remote repository,
which happens when a <emphasis>git-push</emphasis> is done on a local repository.
It executes on the remote repository once after all the refs have
been updated.</simpara>
<simpara>It takes a variable number of parameters, each of which is the
name of ref that was actually updated.</simpara>
<simpara>This hook is meant primarily for notification, and cannot affect
the outcome of <emphasis>git-receive-pack</emphasis>.</simpara>
<simpara>The <emphasis>post-update</emphasis> hook can tell what are the heads that were pushed,
but it does not know what their original and updated values are,
so it is a poor place to do log old..new. The
<link linkend="post-receive"><emphasis>post-receive</emphasis></link> hook does get both original and
updated values of the refs. You might consider it instead if you need
them.</simpara>
<simpara>When enabled, the default <emphasis>post-update</emphasis> hook runs
<emphasis>git-update-server-info</emphasis> to keep the information used by dumb
transports (e.g., HTTP) up-to-date.  If you are publishing
a git repository that is accessible via HTTP, you should
probably enable this hook.</simpara>
<simpara>Both standard output and standard error output are forwarded to
<emphasis>git-send-pack</emphasis> on the other end, so you can simply <literal>echo</literal> messages
for the user.</simpara>
</simplesect>
<simplesect id="_pre_auto_gc">
<title>pre-auto-gc</title>
<simpara>This hook is invoked by <emphasis>git-gc --auto</emphasis>. It takes no parameter, and
exiting with non-zero status from this script causes the <emphasis>git-gc --auto</emphasis>
to abort.</simpara>
</simplesect>
<simplesect id="_git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)"/> suite</simpara>
</simplesect>
</article>
