Sub topic 3
How a hook runs: executable bit, arguments and exit status
This chapter assumes you know the basic Git workflow (add, commit, push) and the difference between the staging area and the working tree. You should also be comfortable writing a short shell script.
What Git does when it runs a hook
A hook is an ordinary file. Git looks for a file whose name matches the hook point, such as pre-commit, in the hooks directory. By default that directory is .git/hooks. If the file exists and is executable, Git runs it as a child process. If not, Git skips it.
Three facts decide how a hook behaves:
- The file must be named exactly after the hook point, with no extension.
- The file must have the executable bit set.
- The exit status of the process tells Git whether to continue.
The executable bit
On Linux and macOS, a file is runnable only if its executable permission is set. A script you create with an editor usually lacks it. Git then ignores the hook, usually without any message.
chmod +x .git/hooks/pre-commit
ls -l .git/hooks/pre-commitThe ls -l output should show an x in the permission string, for example -rwxr-xr-x.
The first line of the script, the shebang, tells the operating system which interpreter to use. Git does not read it. Without a shebang, the file may fail to run or may be run by the wrong shell.
Arguments and standard input
Git passes information to a hook in two ways: command-line arguments and standard input. What you get depends on the hook.
| Hook | Arguments | Standard input |
|---|---|---|
| `pre-commit` | none | none |
| `commit-msg` | path to the file with the proposed message | none |
| `pre-push` | remote name and remote URL | one line per ref being pushed |
pre-commit receives nothing, so it has to ask Git what is staged, for example with git diff --cached. commit-msg receives a file path as $1, and you read the message from that file.
Git also sets the working directory. For commit-related hooks it is the top of the working tree, so relative paths behave the same no matter where you ran git commit.
Exit status decides the outcome
Every process ends with an integer exit status. Zero means success. Any other value means failure. Git applies this rule to hooks that can block an action:
- Exit
0: Git continues with the commit or push. - Exit non-zero: Git aborts it.
Output is separate from status. Text written to standard output or standard error appears in your terminal, but it does not affect the decision. A hook can print an alarming warning and still allow the commit if it exits 0.
Here is a hook that fails on purpose, so you can see the abort:
#!/bin/sh
# .git/hooks/pre-commit
echo "pre-commit: blocking this commit as a test." >&2
exit 1After chmod +x, run git commit. Git prints your message and stops. No commit is created, and your staged changes stay in place.
A common mistake: losing the failing status
In a shell script, the exit status of the script is the status of the last command that ran, unless you call exit yourself. This can hide a failure:
#!/bin/sh
# Looks like a check, but always exits 0.
git diff --cached --check
echo "checks done"The echo succeeds, so the script exits 0 even when git diff --cached --check found problems. The fix is to test the command and exit explicitly:
#!/bin/sh
if ! git diff --cached --check; then
echo "pre-commit: whitespace errors found." >&2
exit 1
fi
exit 0Adding set -e at the top is another option. It makes the script stop at the first failing command. It has well-known edge cases inside pipelines and conditions, so an explicit if and exit is easier to reason about.
Hooks that cannot block
Not every hook can stop an action. pre-commit, commit-msg and pre-push can, because they run before the action completes. Hooks such as post-commit run afterward. Their exit status is ignored for the purpose of undoing the commit.
Exercise
You wrote .git/hooks/pre-commit containing a shebang and exit 1, but commits still succeed and print nothing. Give two likely causes and one command to check the first.
Show answer
Likely causes: the file lacks the executable bit, or the file name is wrong (for example pre-commit.sh or pre-commit.sample). Check the permissions with ls -l .git/hooks/pre-commit and look for an x in the permission string. Fix it with chmod +x .git/hooks/pre-commit. Another possible cause is that core.hooksPath points to a different directory, which you can check with git config core.hooksPath.
Exercise
A pre-commit script runs git diff --cached --check and then echo "done". Why does it never block a commit, and how do you fix it?
Show answer
The script's exit status is the status of its last command, echo, which is always 0. Fix it by testing the check with if ! git diff --cached --check; then exit 1; fi, then ending with exit 0.