]>
Commit | Line | Data |
---|---|---|
72e9340c | 1 | Hooks used by git |
6d35cc76 | 2 | ================= |
6d35cc76 JH |
3 | |
4 | Hooks are little scripts you can place in `$GIT_DIR/hooks` | |
5 | directory to trigger action at certain points. When | |
5c94f87e | 6 | `git-init` is run, a handful example hooks are copied in the |
6d35cc76 | 7 | `hooks` directory of the new repository, but by default they are |
45ad9b50 | 8 | all disabled. To enable a hook, make it executable with `chmod +x`. |
6d35cc76 JH |
9 | |
10 | This document describes the currently defined hooks. | |
11 | ||
12 | applypatch-msg | |
13 | -------------- | |
14 | ||
59c8e2cb | 15 | This hook is invoked by `git-am` script. It takes a single |
6d35cc76 | 16 | parameter, the name of the file that holds the proposed commit |
45ad9b50 | 17 | log message. Exiting with non-zero status causes |
59c8e2cb | 18 | `git-am` to abort before applying the patch. |
6d35cc76 JH |
19 | |
20 | The hook is allowed to edit the message file in place, and can | |
21 | be used to normalize the message into some project standard | |
22 | format (if the project has one). It can also be used to refuse | |
23 | the commit after inspecting the message file. | |
24 | ||
45ad9b50 JF |
25 | The default 'applypatch-msg' hook, when enabled, runs the |
26 | 'commit-msg' hook, if the latter is enabled. | |
6d35cc76 JH |
27 | |
28 | pre-applypatch | |
29 | -------------- | |
30 | ||
47458bb9 CC |
31 | This hook is invoked by `git-am`. It takes no parameter, and is |
32 | invoked after the patch is applied, but before a commit is made. | |
33 | ||
34 | If it exits with non-zero status, then the working tree will not be | |
35 | committed after applying the patch. | |
6d35cc76 JH |
36 | |
37 | It can be used to inspect the current working tree and refuse to | |
38 | make a commit if it does not pass certain test. | |
39 | ||
45ad9b50 JF |
40 | The default 'pre-applypatch' hook, when enabled, runs the |
41 | 'pre-commit' hook, if the latter is enabled. | |
6d35cc76 JH |
42 | |
43 | post-applypatch | |
44 | --------------- | |
45 | ||
59c8e2cb | 46 | This hook is invoked by `git-am`. It takes no parameter, |
6d35cc76 JH |
47 | and is invoked after the patch is applied and a commit is made. |
48 | ||
49 | This hook is meant primarily for notification, and cannot affect | |
59c8e2cb | 50 | the outcome of `git-am`. |
6d35cc76 JH |
51 | |
52 | pre-commit | |
53 | ---------- | |
54 | ||
215a7ad1 | 55 | This hook is invoked by `git-commit`, and can be bypassed |
e1ccf53a | 56 | with `\--no-verify` option. It takes no parameter, and is |
6d35cc76 JH |
57 | invoked before obtaining the proposed commit log message and |
58 | making a commit. Exiting with non-zero status from this script | |
215a7ad1 | 59 | causes the `git-commit` to abort. |
6d35cc76 | 60 | |
45ad9b50 | 61 | The default 'pre-commit' hook, when enabled, catches introduction |
6d35cc76 | 62 | of lines with trailing whitespaces and aborts the commit when |
45ad9b50 | 63 | such a line is found. |
6d35cc76 | 64 | |
406400ce PB |
65 | All the `git-commit` hooks are invoked with the environment |
66 | variable `GIT_EDITOR=:` if the command will not bring up an editor | |
67 | to modify the commit message. | |
68 | ||
8089c85b PB |
69 | prepare-commit-msg |
70 | ------------------ | |
71 | ||
72 | This hook is invoked by `git-commit` right after preparing the | |
73 | default log message, and before the editor is started. | |
74 | ||
75 | It takes one to three parameters. The first is the name of the file | |
76 | that the commit log message. The second is the source of the commit | |
77 | message, and can be: `message` (if a `\-m` or `\-F` option was | |
78 | given); `template` (if a `\-t` option was given or the | |
79 | configuration option `commit.template` is set); `merge` (if the | |
80 | commit is a merge or a `.git/MERGE_MSG` file exists); `squash` | |
81 | (if a `.git/SQUASH_MSG` file exists); or `commit`, followed by | |
82 | a commit SHA1 (if a `\-c`, `\-C` or `\--amend` option was given). | |
83 | ||
84 | If the exit status is non-zero, `git-commit` will abort. | |
85 | ||
86 | The purpose of the hook is to edit the message file in place, and | |
87 | it is not suppressed by the `\--no-verify` option. A non-zero exit | |
88 | means a failure of the hook and aborts the commit. It should not | |
89 | be used as replacement for pre-commit hook. | |
90 | ||
91 | The sample `prepare-commit-msg` hook that comes with git comments | |
92 | out the `Conflicts:` part of a merge's commit message. | |
93 | ||
6d35cc76 JH |
94 | commit-msg |
95 | ---------- | |
96 | ||
215a7ad1 | 97 | This hook is invoked by `git-commit`, and can be bypassed |
e1ccf53a | 98 | with `\--no-verify` option. It takes a single parameter, the |
6d35cc76 | 99 | name of the file that holds the proposed commit log message. |
215a7ad1 | 100 | Exiting with non-zero status causes the `git-commit` to |
6d35cc76 JH |
101 | abort. |
102 | ||
103 | The hook is allowed to edit the message file in place, and can | |
104 | be used to normalize the message into some project standard | |
105 | format (if the project has one). It can also be used to refuse | |
106 | the commit after inspecting the message file. | |
107 | ||
45ad9b50 JF |
108 | The default 'commit-msg' hook, when enabled, detects duplicate |
109 | "Signed-off-by" lines, and aborts the commit if one is found. | |
6d35cc76 JH |
110 | |
111 | post-commit | |
112 | ----------- | |
113 | ||
215a7ad1 | 114 | This hook is invoked by `git-commit`. It takes no |
6d35cc76 JH |
115 | parameter, and is invoked after a commit is made. |
116 | ||
117 | This hook is meant primarily for notification, and cannot affect | |
215a7ad1 | 118 | the outcome of `git-commit`. |
6d35cc76 | 119 | |
1abbe475 JE |
120 | post-checkout |
121 | ----------- | |
122 | ||
123 | This hook is invoked when a `git-checkout` is run after having updated the | |
124 | worktree. The hook is given three parameters: the ref of the previous HEAD, | |
125 | the ref of the new HEAD (which may or may not have changed), and a flag | |
126 | indicating whether the checkout was a branch checkout (changing branches, | |
127 | flag=1) or a file checkout (retrieving a file from the index, flag=0). | |
128 | This hook cannot affect the outcome of `git-checkout`. | |
129 | ||
130 | This hook can be used to perform repository validity checks, auto-display | |
131 | differences from the previous HEAD if different, or set working dir metadata | |
132 | properties. | |
133 | ||
46232915 JE |
134 | post-merge |
135 | ----------- | |
136 | ||
137 | This hook is invoked by `git-merge`, which happens when a `git pull` | |
138 | is done on a local repository. The hook takes a single parameter, a status | |
139 | flag specifying whether or not the merge being done was a squash merge. | |
140 | This hook cannot affect the outcome of `git-merge`. | |
141 | ||
142 | This hook can be used in conjunction with a corresponding pre-commit hook to | |
143 | save and restore any form of metadata associated with the working tree | |
af6fb4c8 JE |
144 | (eg: permissions/ownership, ACLS, etc). See contrib/hooks/setgitperms.perl |
145 | for an example of how to do this. | |
46232915 | 146 | |
cbb84e5d JH |
147 | [[pre-receive]] |
148 | pre-receive | |
149 | ----------- | |
150 | ||
151 | This hook is invoked by `git-receive-pack` on the remote repository, | |
152 | which happens when a `git push` is done on a local repository. | |
153 | Just before starting to update refs on the remote repository, the | |
154 | pre-receive hook is invoked. Its exit status determines the success | |
155 | or failure of the update. | |
156 | ||
157 | This hook executes once for the receive operation. It takes no | |
158 | arguments, but for each ref to be updated it receives on standard | |
159 | input a line of the format: | |
160 | ||
161 | <old-value> SP <new-value> SP <ref-name> LF | |
162 | ||
163 | where `<old-value>` is the old object name stored in the ref, | |
164 | `<new-value>` is the new object name to be stored in the ref and | |
165 | `<ref-name>` is the full name of the ref. | |
166 | When creating a new ref, `<old-value>` is 40 `0`. | |
167 | ||
168 | If the hook exits with non-zero status, none of the refs will be | |
169 | updated. If the hook exits with zero, updating of individual refs can | |
170 | still be prevented by the <<update,'update'>> hook. | |
171 | ||
24a0d61e JH |
172 | Both standard output and standard error output are forwarded to |
173 | `git-send-pack` on the other end, so you can simply `echo` messages | |
174 | for the user. | |
cbb84e5d JH |
175 | |
176 | [[update]] | |
6d35cc76 JH |
177 | update |
178 | ------ | |
179 | ||
6250ad1e | 180 | This hook is invoked by `git-receive-pack` on the remote repository, |
45ad9b50 | 181 | which happens when a `git push` is done on a local repository. |
6250ad1e | 182 | Just before updating the ref on the remote repository, the update hook |
37425065 | 183 | is invoked. Its exit status determines the success or failure of |
6250ad1e JL |
184 | the ref update. |
185 | ||
186 | The hook executes once for each ref to be updated, and takes | |
187 | three parameters: | |
45ad9b50 JF |
188 | |
189 | - the name of the ref being updated, | |
190 | - the old object name stored in the ref, | |
191 | - and the new objectname to be stored in the ref. | |
6250ad1e JL |
192 | |
193 | A zero exit from the update hook allows the ref to be updated. | |
194 | Exiting with a non-zero status prevents `git-receive-pack` | |
cbb84e5d | 195 | from updating that ref. |
6250ad1e JL |
196 | |
197 | This hook can be used to prevent 'forced' update on certain refs by | |
6d35cc76 JH |
198 | making sure that the object name is a commit object that is a |
199 | descendant of the commit object named by the old object name. | |
6250ad1e JL |
200 | That is, to enforce a "fast forward only" policy. |
201 | ||
202 | It could also be used to log the old..new status. However, it | |
203 | does not know the entire set of branches, so it would end up | |
cbb84e5d JH |
204 | firing one e-mail per ref when used naively, though. The |
205 | <<post-receive,'post-receive'>> hook is more suited to that. | |
6250ad1e | 206 | |
6d35cc76 JH |
207 | Another use suggested on the mailing list is to use this hook to |
208 | implement access control which is finer grained than the one | |
209 | based on filesystem group. | |
210 | ||
24a0d61e JH |
211 | Both standard output and standard error output are forwarded to |
212 | `git-send-pack` on the other end, so you can simply `echo` messages | |
213 | for the user. | |
3aadad1b | 214 | |
cbb84e5d JH |
215 | The default 'update' hook, when enabled--and with |
216 | `hooks.allowunannotated` config option turned on--prevents | |
217 | unannotated tags to be pushed. | |
218 | ||
219 | [[post-receive]] | |
220 | post-receive | |
221 | ------------ | |
6250ad1e | 222 | |
cbb84e5d JH |
223 | This hook is invoked by `git-receive-pack` on the remote repository, |
224 | which happens when a `git push` is done on a local repository. | |
225 | It executes on the remote repository once after all the refs have | |
226 | been updated. | |
227 | ||
228 | This hook executes once for the receive operation. It takes no | |
24a0d61e JH |
229 | arguments, but gets the same information as the |
230 | <<pre-receive,'pre-receive'>> | |
cbb84e5d JH |
231 | hook does on its standard input. |
232 | ||
233 | This hook does not affect the outcome of `git-receive-pack`, as it | |
234 | is called after the real work is done. | |
235 | ||
02783075 | 236 | This supersedes the <<post-update,'post-update'>> hook in that it gets |
24a0d61e JH |
237 | both old and new values of all the refs in addition to their |
238 | names. | |
cbb84e5d | 239 | |
24a0d61e JH |
240 | Both standard output and standard error output are forwarded to |
241 | `git-send-pack` on the other end, so you can simply `echo` messages | |
242 | for the user. | |
cbb84e5d JH |
243 | |
244 | The default 'post-receive' hook is empty, but there is | |
245 | a sample script `post-receive-email` provided in the `contrib/hooks` | |
246 | directory in git distribution, which implements sending commit | |
247 | emails. | |
248 | ||
249 | [[post-update]] | |
6d35cc76 JH |
250 | post-update |
251 | ----------- | |
252 | ||
6250ad1e | 253 | This hook is invoked by `git-receive-pack` on the remote repository, |
45ad9b50 | 254 | which happens when a `git push` is done on a local repository. |
6250ad1e JL |
255 | It executes on the remote repository once after all the refs have |
256 | been updated. | |
257 | ||
258 | It takes a variable number of parameters, each of which is the | |
259 | name of ref that was actually updated. | |
6d35cc76 JH |
260 | |
261 | This hook is meant primarily for notification, and cannot affect | |
262 | the outcome of `git-receive-pack`. | |
263 | ||
45ad9b50 | 264 | The 'post-update' hook can tell what are the heads that were pushed, |
6250ad1e | 265 | but it does not know what their original and updated values are, |
24a0d61e JH |
266 | so it is a poor place to do log old..new. The |
267 | <<post-receive,'post-receive'>> hook does get both original and | |
268 | updated values of the refs. You might consider it instead if you need | |
269 | them. | |
cbb84e5d | 270 | |
45ad9b50 | 271 | When enabled, the default 'post-update' hook runs |
6d35cc76 | 272 | `git-update-server-info` to keep the information used by dumb |
45ad9b50 JF |
273 | transports (e.g., HTTP) up-to-date. If you are publishing |
274 | a git repository that is accessible via HTTP, you should | |
6250ad1e | 275 | probably enable this hook. |
3aadad1b | 276 | |
cbb84e5d | 277 | Both standard output and standard error output are forwarded to |
24a0d61e JH |
278 | `git-send-pack` on the other end, so you can simply `echo` messages |
279 | for the user. | |
0b85d926 MV |
280 | |
281 | pre-auto-gc | |
282 | ----------- | |
283 | ||
284 | This hook is invoked by `git-gc --auto`. It takes no parameter, and | |
285 | exiting with non-zero status from this script causes the `git-gc --auto` | |
286 | to abort. |