Update git documentation
[TortoiseGit.git] / doc / source / en / TortoiseGit / git_doc / githooks.xml
blob2d094bff3609e655f3a2fc9875041e5c5bb32845
1 <?xml version="1.0" encoding="UTF-8"?>\r
2 <!DOCTYPE sect2 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">\r
3 \r
4 <sect2 lang="en" id="githooks(5)">\r
5     <title>githooks(5)</title>\r
6 <indexterm>\r
7 <primary>githooks(5)</primary>\r
8 </indexterm>\r
9 <simplesect id="githooks(5)__name">\r
10 <title>NAME</title>\r
11 <simpara>githooks - Hooks used by Git</simpara>\r
12 </simplesect>\r
13 <simplesect id="githooks(5)__synopsis">\r
14 <title>SYNOPSIS</title>\r
15 <simpara>$GIT_DIR/hooks/*</simpara>\r
16 </simplesect>\r
17 <simplesect id="githooks(5)__description">\r
18 <title>DESCRIPTION</title>\r
19 <simpara>Hooks are little scripts you can place in <emphasis>$GIT_DIR/hooks</emphasis>\r
20 directory to trigger action at certain points.  When\r
21 <emphasis>git init</emphasis> is run, a handful of example hooks are copied into the\r
22 <emphasis>hooks</emphasis> directory of the new repository, but by default they are\r
23 all disabled.  To enable a hook, rename it by removing its <emphasis>.sample</emphasis>\r
24 suffix.</simpara>\r
25 <note><simpara>It is also a requirement for a given hook to be executable.\r
26 However - in a freshly initialized repository - the <emphasis>.sample</emphasis> files are\r
27 executable by default.</simpara></note>\r
28 <simpara>This document describes the currently defined hooks.</simpara>\r
29 </simplesect>\r
30 <simplesect id="githooks(5)__hooks">\r
31 <title>HOOKS</title>\r
32 <section id="githooks(5)__applypatch_msg">\r
33 <title>applypatch-msg</title>\r
34 <simpara>This hook is invoked by <emphasis>git am</emphasis> script.  It takes a single\r
35 parameter, the name of the file that holds the proposed commit\r
36 log message.  Exiting with non-zero status causes\r
37 <emphasis>git am</emphasis> to abort before applying the patch.</simpara>\r
38 <simpara>The hook is allowed to edit the message file in place, and can\r
39 be used to normalize the message into some project standard\r
40 format (if the project has one). It can also be used to refuse\r
41 the commit after inspecting the message file.</simpara>\r
42 <simpara>The default <emphasis>applypatch-msg</emphasis> hook, when enabled, runs the\r
43 <emphasis>commit-msg</emphasis> hook, if the latter is enabled.</simpara>\r
44 </section>\r
45 <section id="githooks(5)__pre_applypatch">\r
46 <title>pre-applypatch</title>\r
47 <simpara>This hook is invoked by <emphasis>git am</emphasis>.  It takes no parameter, and is\r
48 invoked after the patch is applied, but before a commit is made.</simpara>\r
49 <simpara>If it exits with non-zero status, then the working tree will not be\r
50 committed after applying the patch.</simpara>\r
51 <simpara>It can be used to inspect the current working tree and refuse to\r
52 make a commit if it does not pass certain test.</simpara>\r
53 <simpara>The default <emphasis>pre-applypatch</emphasis> hook, when enabled, runs the\r
54 <emphasis>pre-commit</emphasis> hook, if the latter is enabled.</simpara>\r
55 </section>\r
56 <section id="githooks(5)__post_applypatch">\r
57 <title>post-applypatch</title>\r
58 <simpara>This hook is invoked by <emphasis>git am</emphasis>.  It takes no parameter,\r
59 and is invoked after the patch is applied and a commit is made.</simpara>\r
60 <simpara>This hook is meant primarily for notification, and cannot affect\r
61 the outcome of <emphasis>git am</emphasis>.</simpara>\r
62 </section>\r
63 <section id="githooks(5)__pre_commit">\r
64 <title>pre-commit</title>\r
65 <simpara>This hook is invoked by <emphasis>git commit</emphasis>, and can be bypassed\r
66 with <emphasis>--no-verify</emphasis> option.  It takes no parameter, and is\r
67 invoked before obtaining the proposed commit log message and\r
68 making a commit.  Exiting with non-zero status from this script\r
69 causes the <emphasis>git commit</emphasis> to abort.</simpara>\r
70 <simpara>The default <emphasis>pre-commit</emphasis> hook, when enabled, catches introduction\r
71 of lines with trailing whitespaces and aborts the commit when\r
72 such a line is found.</simpara>\r
73 <simpara>All the <emphasis>git commit</emphasis> hooks are invoked with the environment\r
74 variable <emphasis>GIT_EDITOR=:</emphasis> if the command will not bring up an editor\r
75 to modify the commit message.</simpara>\r
76 </section>\r
77 <section id="githooks(5)__prepare_commit_msg">\r
78 <title>prepare-commit-msg</title>\r
79 <simpara>This hook is invoked by <emphasis>git commit</emphasis> right after preparing the\r
80 default log message, and before the editor is started.</simpara>\r
81 <simpara>It takes one to three parameters.  The first is the name of the file\r
82 that contains the commit log message.  The second is the source of the commit\r
83 message, and can be: <emphasis>message</emphasis> (if a <emphasis>-m</emphasis> or <emphasis>-F</emphasis> option was\r
84 given); <emphasis>template</emphasis> (if a <emphasis>-t</emphasis> option was given or the\r
85 configuration option <emphasis>commit.template</emphasis> is set); <emphasis>merge</emphasis> (if the\r
86 commit is a merge or a <emphasis>.git/MERGE_MSG</emphasis> file exists); <emphasis>squash</emphasis>\r
87 (if a <emphasis>.git/SQUASH_MSG</emphasis> file exists); or <emphasis>commit</emphasis>, followed by\r
88 a commit SHA-1 (if a <emphasis>-c</emphasis>, <emphasis>-C</emphasis> or <emphasis>--amend</emphasis> option was given).</simpara>\r
89 <simpara>If the exit status is non-zero, <emphasis>git commit</emphasis> will abort.</simpara>\r
90 <simpara>The purpose of the hook is to edit the message file in place, and\r
91 it is not suppressed by the <emphasis>--no-verify</emphasis> option.  A non-zero exit\r
92 means a failure of the hook and aborts the commit.  It should not\r
93 be used as replacement for pre-commit hook.</simpara>\r
94 <simpara>The sample <emphasis>prepare-commit-msg</emphasis> hook that comes with Git comments\r
95 out the <emphasis>Conflicts:</emphasis> part of a merge's commit message.</simpara>\r
96 </section>\r
97 <section id="githooks(5)__commit_msg">\r
98 <title>commit-msg</title>\r
99 <simpara>This hook is invoked by <emphasis>git commit</emphasis>, and can be bypassed\r
100 with <emphasis>--no-verify</emphasis> option.  It takes a single parameter, the\r
101 name of the file that holds the proposed commit log message.\r
102 Exiting with non-zero status causes the <emphasis>git commit</emphasis> to\r
103 abort.</simpara>\r
104 <simpara>The hook is allowed to edit the message file in place, and can\r
105 be used to normalize the message into some project standard\r
106 format (if the project has one). It can also be used to refuse\r
107 the commit after inspecting the message file.</simpara>\r
108 <simpara>The default <emphasis>commit-msg</emphasis> hook, when enabled, detects duplicate\r
109 "Signed-off-by" lines, and aborts the commit if one is found.</simpara>\r
110 </section>\r
111 <section id="githooks(5)__post_commit">\r
112 <title>post-commit</title>\r
113 <simpara>This hook is invoked by <emphasis>git commit</emphasis>.  It takes no\r
114 parameter, and is invoked after a commit is made.</simpara>\r
115 <simpara>This hook is meant primarily for notification, and cannot affect\r
116 the outcome of <emphasis>git commit</emphasis>.</simpara>\r
117 </section>\r
118 <section id="githooks(5)__pre_rebase">\r
119 <title>pre-rebase</title>\r
120 <simpara>This hook is called by <emphasis>git rebase</emphasis> and can be used to prevent a\r
121 branch from getting rebased.  The hook may be called with one or\r
122 two parameters.  The first parameter is the upstream from which\r
123 the series was forked.  The second parameter is the branch being\r
124 rebased, and is not set when rebasing the current branch.</simpara>\r
125 </section>\r
126 <section id="githooks(5)__post_checkout">\r
127 <title>post-checkout</title>\r
128 <simpara>This hook is invoked when a <emphasis>git checkout</emphasis> is run after having updated the\r
129 worktree.  The hook is given three parameters: the ref of the previous HEAD,\r
130 the ref of the new HEAD (which may or may not have changed), and a flag\r
131 indicating whether the checkout was a branch checkout (changing branches,\r
132 flag=1) or a file checkout (retrieving a file from the index, flag=0).\r
133 This hook cannot affect the outcome of <emphasis>git checkout</emphasis>.</simpara>\r
134 <simpara>It is also run after <emphasis>git clone</emphasis>, unless the --no-checkout (-n) option is\r
135 used. The first parameter given to the hook is the null-ref, the second the\r
136 ref of the new HEAD and the flag is always 1.</simpara>\r
137 <simpara>This hook can be used to perform repository validity checks, auto-display\r
138 differences from the previous HEAD if different, or set working dir metadata\r
139 properties.</simpara>\r
140 </section>\r
141 <section id="githooks(5)__post_merge">\r
142 <title>post-merge</title>\r
143 <simpara>This hook is invoked by <emphasis>git merge</emphasis>, which happens when a <emphasis>git pull</emphasis>\r
144 is done on a local repository.  The hook takes a single parameter, a status\r
145 flag specifying whether or not the merge being done was a squash merge.\r
146 This hook cannot affect the outcome of <emphasis>git merge</emphasis> and is not executed,\r
147 if the merge failed due to conflicts.</simpara>\r
148 <simpara>This hook can be used in conjunction with a corresponding pre-commit hook to\r
149 save and restore any form of metadata associated with the working tree\r
150 (e.g.: permissions/ownership, ACLS, etc).  See contrib/hooks/setgitperms.perl\r
151 for an example of how to do this.</simpara>\r
152 </section>\r
153 <section id="githooks(5)__pre_push">\r
154 <title>pre-push</title>\r
155 <simpara>This hook is called by <emphasis>git push</emphasis> and can be used to prevent a push from taking\r
156 place.  The hook is called with two parameters which provide the name and\r
157 location of the destination remote, if a named remote is not being used both\r
158 values will be the same.</simpara>\r
159 <simpara>Information about what is to be pushed is provided on the hook's standard\r
160 input with lines of the form:</simpara>\r
161 <literallayout class="monospaced">&lt;local ref&gt; SP &lt;local sha1&gt; SP &lt;remote ref&gt; SP &lt;remote sha1&gt; LF</literallayout>\r
162 <simpara>For instance, if the command <literal>git push origin master:foreign</literal> were run the\r
163 hook would receive a line like the following:</simpara>\r
164 <literallayout class="monospaced">refs/heads/master 67890 refs/heads/foreign 12345</literallayout>\r
165 <simpara>although the full, 40-character SHA-1s would be supplied.  If the foreign ref\r
166 does not yet exist the <emphasis>&lt;remote SHA-1&gt;</emphasis> will be 40 <emphasis>0</emphasis>.  If a ref is to be\r
167 deleted, the <emphasis>&lt;local ref&gt;</emphasis> will be supplied as <emphasis>(delete)</emphasis> and the <emphasis>&lt;local\r
168 SHA-1&gt;</emphasis> will be 40 <emphasis>0</emphasis>.  If the local commit was specified by something other\r
169 than a name which could be expanded (such as <emphasis>HEAD~</emphasis>, or a SHA-1) it will be\r
170 supplied as it was originally given.</simpara>\r
171 <simpara>If this hook exits with a non-zero status, <emphasis>git push</emphasis> will abort without\r
172 pushing anything.  Information about why the push is rejected may be sent\r
173 to the user by writing to standard error.</simpara>\r
174 </section>\r
175 <section id="githooks(5)_pre-receive">\r
176 <title>pre-receive</title>\r
177 <simpara>This hook is invoked by <emphasis>git-receive-pack</emphasis> on the remote repository,\r
178 which happens when a <emphasis>git push</emphasis> is done on a local repository.\r
179 Just before starting to update refs on the remote repository, the\r
180 pre-receive hook is invoked.  Its exit status determines the success\r
181 or failure of the update.</simpara>\r
182 <simpara>This hook executes once for the receive operation. It takes no\r
183 arguments, but for each ref to be updated it receives on standard\r
184 input a line of the format:</simpara>\r
185 <literallayout class="monospaced">&lt;old-value&gt; SP &lt;new-value&gt; SP &lt;ref-name&gt; LF</literallayout>\r
186 <simpara>where <emphasis>&lt;old-value&gt;</emphasis> is the old object name stored in the ref,\r
187 <emphasis>&lt;new-value&gt;</emphasis> is the new object name to be stored in the ref and\r
188 <emphasis>&lt;ref-name&gt;</emphasis> is the full name of the ref.\r
189 When creating a new ref, <emphasis>&lt;old-value&gt;</emphasis> is 40 <emphasis>0</emphasis>.</simpara>\r
190 <simpara>If the hook exits with non-zero status, none of the refs will be\r
191 updated. If the hook exits with zero, updating of individual refs can\r
192 still be prevented by the <link linkend="githooks(5)_update"><emphasis>update</emphasis></link> hook.</simpara>\r
193 <simpara>Both standard output and standard error output are forwarded to\r
194 <emphasis>git send-pack</emphasis> on the other end, so you can simply <emphasis>echo</emphasis> messages\r
195 for the user.</simpara>\r
196 </section>\r
197 <section id="githooks(5)_update">\r
198 <title>update</title>\r
199 <simpara>This hook is invoked by <emphasis>git-receive-pack</emphasis> on the remote repository,\r
200 which happens when a <emphasis>git push</emphasis> is done on a local repository.\r
201 Just before updating the ref on the remote repository, the update hook\r
202 is invoked.  Its exit status determines the success or failure of\r
203 the ref update.</simpara>\r
204 <simpara>The hook executes once for each ref to be updated, and takes\r
205 three parameters:</simpara>\r
206 <itemizedlist>\r
207 <listitem>\r
208 <simpara>\r
209 the name of the ref being updated,\r
210 </simpara>\r
211 </listitem>\r
212 <listitem>\r
213 <simpara>\r
214 the old object name stored in the ref,\r
215 </simpara>\r
216 </listitem>\r
217 <listitem>\r
218 <simpara>\r
219 and the new object name to be stored in the ref.\r
220 </simpara>\r
221 </listitem>\r
222 </itemizedlist>\r
223 <simpara>A zero exit from the update hook allows the ref to be updated.\r
224 Exiting with a non-zero status prevents <emphasis>git-receive-pack</emphasis>\r
225 from updating that ref.</simpara>\r
226 <simpara>This hook can be used to prevent <emphasis>forced</emphasis> update on certain refs by\r
227 making sure that the object name is a commit object that is a\r
228 descendant of the commit object named by the old object name.\r
229 That is, to enforce a "fast-forward only" policy.</simpara>\r
230 <simpara>It could also be used to log the old..new status.  However, it\r
231 does not know the entire set of branches, so it would end up\r
232 firing one e-mail per ref when used naively, though.  The\r
233 <link linkend="githooks(5)_post-receive"><emphasis>post-receive</emphasis></link> hook is more suited to that.</simpara>\r
234 <simpara>Another use suggested on the mailing list is to use this hook to\r
235 implement access control which is finer grained than the one\r
236 based on filesystem group.</simpara>\r
237 <simpara>Both standard output and standard error output are forwarded to\r
238 <emphasis>git send-pack</emphasis> on the other end, so you can simply <emphasis>echo</emphasis> messages\r
239 for the user.</simpara>\r
240 <simpara>The default <emphasis>update</emphasis> hook, when enabled--and with\r
241 <emphasis>hooks.allowunannotated</emphasis> config option unset or set to false--prevents\r
242 unannotated tags to be pushed.</simpara>\r
243 </section>\r
244 <section id="githooks(5)_post-receive">\r
245 <title>post-receive</title>\r
246 <simpara>This hook is invoked by <emphasis>git-receive-pack</emphasis> on the remote repository,\r
247 which happens when a <emphasis>git push</emphasis> is done on a local repository.\r
248 It executes on the remote repository once after all the refs have\r
249 been updated.</simpara>\r
250 <simpara>This hook executes once for the receive operation.  It takes no\r
251 arguments, but gets the same information as the\r
252 <link linkend="githooks(5)_pre-receive"><emphasis>pre-receive</emphasis></link>\r
253 hook does on its standard input.</simpara>\r
254 <simpara>This hook does not affect the outcome of <emphasis>git-receive-pack</emphasis>, as it\r
255 is called after the real work is done.</simpara>\r
256 <simpara>This supersedes the <link linkend="githooks(5)_post-update"><emphasis>post-update</emphasis></link> hook in that it gets\r
257 both old and new values of all the refs in addition to their\r
258 names.</simpara>\r
259 <simpara>Both standard output and standard error output are forwarded to\r
260 <emphasis>git send-pack</emphasis> on the other end, so you can simply <emphasis>echo</emphasis> messages\r
261 for the user.</simpara>\r
262 <simpara>The default <emphasis>post-receive</emphasis> hook is empty, but there is\r
263 a sample script <emphasis>post-receive-email</emphasis> provided in the <emphasis>contrib/hooks</emphasis>\r
264 directory in Git distribution, which implements sending commit\r
265 emails.</simpara>\r
266 </section>\r
267 <section id="githooks(5)_post-update">\r
268 <title>post-update</title>\r
269 <simpara>This hook is invoked by <emphasis>git-receive-pack</emphasis> on the remote repository,\r
270 which happens when a <emphasis>git push</emphasis> is done on a local repository.\r
271 It executes on the remote repository once after all the refs have\r
272 been updated.</simpara>\r
273 <simpara>It takes a variable number of parameters, each of which is the\r
274 name of ref that was actually updated.</simpara>\r
275 <simpara>This hook is meant primarily for notification, and cannot affect\r
276 the outcome of <emphasis>git-receive-pack</emphasis>.</simpara>\r
277 <simpara>The <emphasis>post-update</emphasis> hook can tell what are the heads that were pushed,\r
278 but it does not know what their original and updated values are,\r
279 so it is a poor place to do log old..new. The\r
280 <link linkend="githooks(5)_post-receive"><emphasis>post-receive</emphasis></link> hook does get both original and\r
281 updated values of the refs. You might consider it instead if you need\r
282 them.</simpara>\r
283 <simpara>When enabled, the default <emphasis>post-update</emphasis> hook runs\r
284 <emphasis>git update-server-info</emphasis> to keep the information used by dumb\r
285 transports (e.g., HTTP) up-to-date.  If you are publishing\r
286 a Git repository that is accessible via HTTP, you should\r
287 probably enable this hook.</simpara>\r
288 <simpara>Both standard output and standard error output are forwarded to\r
289 <emphasis>git send-pack</emphasis> on the other end, so you can simply <emphasis>echo</emphasis> messages\r
290 for the user.</simpara>\r
291 </section>\r
292 <section id="githooks(5)__push_to_checkout">\r
293 <title>push-to-checkout</title>\r
294 <simpara>This hook is invoked by <emphasis>git-receive-pack</emphasis> on the remote repository,\r
295 which happens when a <emphasis>git push</emphasis> is done on a local repository, when\r
296 the push tries to update the branch that is currently checked out\r
297 and the <emphasis>receive.denyCurrentBranch</emphasis> configuration variable is set to\r
298 <emphasis>updateInstead</emphasis>.  Such a push by default is refused if the working\r
299 tree and the index of the remote repository has any difference from\r
300 the currently checked out commit; when both the working tree and the\r
301 index match the current commit, they are updated to match the newly\r
302 pushed tip of the branch.  This hook is to be used to override the\r
303 default behaviour.</simpara>\r
304 <simpara>The hook receives the commit with which the tip of the current\r
305 branch is going to be updated.  It can exit with a non-zero status\r
306 to refuse the push (when it does so, it must not modify the index or\r
307 the working tree).  Or it can make any necessary changes to the\r
308 working tree and to the index to bring them to the desired state\r
309 when the tip of the current branch is updated to the new commit, and\r
310 exit with a zero status.</simpara>\r
311 <simpara>For example, the hook can simply run <emphasis>git read-tree -u -m HEAD "$1"</emphasis>\r
312 in order to emulate <emphasis>git fetch</emphasis> that is run in the reverse direction\r
313 with <emphasis>git push</emphasis>, as the two-tree form of <emphasis>read-tree -u -m</emphasis> is\r
314 essentially the same as <emphasis>git checkout</emphasis> that switches branches while\r
315 keeping the local changes in the working tree that do not interfere\r
316 with the difference between the branches.</simpara>\r
317 </section>\r
318 <section id="githooks(5)__pre_auto_gc">\r
319 <title>pre-auto-gc</title>\r
320 <simpara>This hook is invoked by <emphasis>git gc --auto</emphasis>. It takes no parameter, and\r
321 exiting with non-zero status from this script causes the <emphasis>git gc --auto</emphasis>\r
322 to abort.</simpara>\r
323 </section>\r
324 <section id="githooks(5)__post_rewrite">\r
325 <title>post-rewrite</title>\r
326 <simpara>This hook is invoked by commands that rewrite commits (<emphasis>git commit\r
327 --amend</emphasis>, <emphasis>git-rebase</emphasis>; currently <emphasis>git-filter-branch</emphasis> does <emphasis>not</emphasis> call\r
328 it!).  Its first argument denotes the command it was invoked by:\r
329 currently one of <emphasis>amend</emphasis> or <emphasis>rebase</emphasis>.  Further command-dependent\r
330 arguments may be passed in the future.</simpara>\r
331 <simpara>The hook receives a list of the rewritten commits on stdin, in the\r
332 format</simpara>\r
333 <literallayout class="monospaced">&lt;old-sha1&gt; SP &lt;new-sha1&gt; [ SP &lt;extra-info&gt; ] LF</literallayout>\r
334 <simpara>The <emphasis>extra-info</emphasis> is again command-dependent.  If it is empty, the\r
335 preceding SP is also omitted.  Currently, no commands pass any\r
336 <emphasis>extra-info</emphasis>.</simpara>\r
337 <simpara>The hook always runs after the automatic note copying (see\r
338 "notes.rewrite.&lt;command&gt;" in <xref linkend="git-config(1)" />) has happened, and\r
339 thus has access to these notes.</simpara>\r
340 <simpara>The following command-specific comments apply:</simpara>\r
341 <variablelist>\r
342 <varlistentry>\r
343 <term>\r
344 rebase\r
345 </term>\r
346 <listitem>\r
347 <simpara>\r
348         For the <emphasis>squash</emphasis> and <emphasis>fixup</emphasis> operation, all commits that were\r
349         squashed are listed as being rewritten to the squashed commit.\r
350         This means that there will be several lines sharing the same\r
351         <emphasis>new-sha1</emphasis>.\r
352 </simpara>\r
353 <simpara>The commits are guaranteed to be listed in the order that they were\r
354 processed by rebase.</simpara>\r
355 </listitem>\r
356 </varlistentry>\r
357 </variablelist>\r
358 </section>\r
359 </simplesect>\r
360 <simplesect id="githooks(5)__git">\r
361 <title>GIT</title>\r
362 <simpara>Part of the <xref linkend="git(1)" /> suite</simpara>\r
363 </simplesect>\r
364 </sect2>\r