Sub topic 12
Debugging Hooks and Cross-platform Pitfalls
This chapter assumes you can write a pre-commit hook, know the staging area from the working tree, and have shared hooks with core.hooksPath. Here you will learn why hooks fail to run, how to see what they do, and what changes between operating systems.
A hook that never runs
Most hook problems are one of four causes. Check them in this order.
- The file is not executable. Git skips a hook without the executable bit. Recent Git versions print a hint that the hook was ignored. Fix it with
chmod +x. - The name is wrong. The file must be named exactly
pre-commit, with no extension. A file calledpre-commit.shorpre-commit.sampleis never run. - The hook is in the wrong directory. If
core.hooksPathis set, Git ignores.git/hookscompletely. - The hook is not the one you edited. In a clone where nobody ran
git config core.hooksPath .githooks, your shared hooks are inactive.
Run these commands to see what Git will actually use.
git config --get core.hooksPath
git rev-parse --git-path hooks
ls -l "$(git rev-parse --git-path hooks)"If the first command prints nothing, Git uses the default .git/hooks. The second command prints the directory Git resolves for hooks, and the third shows the permission bits.
Seeing what a hook does
A hook runs inside Git, so its output can be easy to miss. Make it talk.
#!/bin/sh
# .githooks/pre-commit
set -eu
[ "${HOOK_DEBUG:-}" = "1" ] && set -x
echo "pre-commit: running in $(pwd)" >&2
git diff --cached --checkRun the hook as HOOK_DEBUG=1 git commit. The set -x line makes the shell print each command before it runs. You can also run the script directly with .githooks/pre-commit and then read echo $? to see the exit status. Git only looks at that status, so a hook that "looks fine" but ends with a failing command still blocks the commit.
You can test the commit flow without creating a commit. Use git commit --dry-run, or run git hook run pre-commit on Git 2.36 and later.
Wrong interpreter and line endings
The first line of a hook, the shebang, chooses the interpreter. Two mistakes are common.
- Windows line endings. If the file has CRLF endings, the shebang becomes
#!/bin/sh\r. Unix systems then reportbad interpreterornot found. Convert the file withdos2unix, or force LF endings in.gitattributes:
.githooks/* text eol=lf- A tool that is not installed. The hook calls
shellcheck,nodeorpython, and the command is missing fromPATH. Failing with "command not found" blocks the commit with a confusing message.
Make missing tools explicit.
#!/bin/sh
# .githooks/pre-commit
if ! command -v shellcheck >/dev/null 2>&1; then
echo "pre-commit: shellcheck is not installed, see README." >&2
exit 1
fiDecide per tool whether a missing dependency should block the commit or only warn. Blocking is safer. Warning is friendlier to new contributors.
Environment differences
Hooks do not always start in the environment of your interactive shell.
- GUI clients. Editors and Git GUIs often start with a smaller
PATHthan your terminal. A tool installed through a version manager such asnvmmay be found in the terminal and missing in the GUI. - Working directory. Git runs most hooks from the top of the working tree. Do not depend on this in scripts you also run by hand. Use
git rev-parse --show-toplevelto find the root. - Git variables. During a commit Git sets variables such as
GIT_INDEX_FILE. If your hook runs anothergitcommand in a different repository, these variables can point it at the wrong index. Unset them withunset GIT_DIR GIT_INDEX_FILEbefore you change directory.
Windows and macOS pitfalls
| Pitfall | Where it appears | Fix |
|---|---|---|
| No executable bit on the file system | Windows without Git Bash | Run `git update-index --chmod=+x .githooks/pre-commit` |
| GNU-only options such as `sed -i` without a suffix | macOS BSD tools | Use portable options, or write the check in Python or Node |
| Case-insensitive file names | macOS and Windows | Keep hook names lowercase and exact |
| Backslash paths | Windows | Use `git` to supply paths, and quote them |
Git for Windows ships its own sh, so a #!/bin/sh hook works there. A hook that needs bash features or Linux utilities does not run in every setup. Keep hooks POSIX-compatible, or move the logic into a language every team member has installed.
Do not rely on that diagram; use this flow instead.
Exercise
A teammate pulls your branch. Your .githooks/pre-commit is in the repository, but their commits are never checked. The file is executable and the name is correct. What is the most likely cause, and how do you confirm it?
Show answer
The teammate never ran git config core.hooksPath .githooks, because that setting is local and is not versioned. Confirm it with git config --get core.hooksPath, which prints nothing when the setting is missing. Running the config command fixes it.