Sub topic 12
Debugging hooks and cross-platform pitfalls
This chapter assumes you can write a pre-commit hook, know that a non-zero exit status blocks the commit, and know how core.hooksPath shares hooks. You should also be comfortable reading shell scripts. Here you will learn how to find out why a hook did not run, why it behaved differently from what you expected, and why it works on one machine and fails on another.
Start with the symptom
Hook problems fall into two groups. Either the hook never ran, or it ran and did the wrong thing. Deciding which one you have saves most of the debugging time.
Confirm whether the hook ran
Git can print every program it starts. Run the commit with tracing on:
GIT_TRACE=1 git commit -m "test hook"If the hook ran, the output contains a line such as run_command: .githooks/pre-commit. If that line is missing, Git never found an executable hook, and the problem is in how the hook is installed. If the line is present, the hook ran and the problem is inside the script.
Hooks that never run
Work through these causes in order. They account for most reports of a hook being ignored.
Wrong location
Git looks for hooks in .git/hooks unless core.hooksPath is set. Check what is in effect and where the setting came from:
git config --show-origin --get core.hooksPath
git rev-parse --git-path hooks
ls -l .githooksThe first command prints nothing if the option is unset. In that case Git uses .git/hooks, and git rev-parse --git-path hooks shows where that is, which matters in linked worktrees. If a global or system config sets core.hooksPath, the --show-origin output shows the file. A global setting silently replaces the hooks of every repository, including the ones you meant to configure locally. A relative core.hooksPath is resolved from the directory where the hook runs, which is the top of the working tree for a normal commit. That is why .githooks works without an absolute path.
Wrong name
The file name must match the hook name exactly: pre-commit, not pre-commit.sh and not precommit. Files that end in .sample are inactive examples. Names are case-sensitive on Linux, so Pre-commit is ignored there even if it works on a case-insensitive macOS volume.
Missing executable bit
If the file is not executable, Git skips it. Recent Git versions print a hint such as The '.githooks/pre-commit' hook was ignored because it's not set as executable. Older versions say nothing. Fix it with chmod +x. On Windows you also need to record the bit in the index, as described later.
The hook does not apply to that command
Not every way of creating a commit runs every hook:
git commit --no-verify(or-n) skipspre-commitandcommit-msg.- A merge that completes without conflicts does not run
pre-commit. Git runspre-merge-commitinstead. git rebasereplays commits without runningpre-commitfor each one.git push --no-verifyskipspre-push.
Check the lifecycle before you conclude that a hook is broken.
Hooks that run but misbehave
Read the exit status
Git only looks at the exit status. A hook that prints an error but then ends with a successful command lets the commit through. Two mistakes cause this:
- A pipeline reports the status of its last command. In
git show ":$f" | shellcheck -, the status comes fromshellcheck. Inshellcheck file | head, it comes fromhead, so failures are hidden. - The script ends with a command that succeeds, such as
echo, without an explicitexit.
To see the real status, run the hook directly and print $?:
.githooks/pre-commit; echo "exit status: $?"Running the hook by hand is often the fastest test. Stage a bad change, run the script, and read the output without the commit machinery around it.
Trace the script
Add an opt-in debug switch so you do not edit the script each time:
#!/bin/sh
# .githooks/pre-commit
# Set HOOK_DEBUG=1 to print every command the hook runs.
[ -n "${HOOK_DEBUG:-}" ] && set -x
if ! git diff --cached --check; then
echo "pre-commit: fix the problems above or commit with --no-verify." >&2
exit 1
fi
exit 0Then run HOOK_DEBUG=1 git commit -m "test". The set -x output goes to standard error and shows each expanded command, which exposes quoting mistakes and empty variables. If you still cannot see why a check fails, run sh -x .githooks/pre-commit from the repository root.
The hook checks the wrong files
A hook must read staged content, not the working tree. Two edge cases make this harder than it looks:
- With
git commit -aorgit commit path, Git builds a temporary index and setsGIT_INDEX_FILEfor the hook. Git commands such asgit diff --cachedandgit show :pathhandle this correctly. Scripts that assume.git/indexis the index do not. - If your hook runs a formatter that rewrites files, the rewrite happens in the working tree. The commit still contains the old staged version unless the hook runs
git addafterwards. Many teams make hooks report problems and leave fixing to the developer, to avoid that surprise.
The environment differs from your terminal
A hook runs with the environment of whatever started Git. In a terminal that is your shell. From an IDE or a Git GUI, it is often a smaller environment that did not read .bashrc or .zshrc. Typical failures are node: command not found when Node is installed through a version manager, or the wrong Python version.
To find out what the hook sees, print it:
echo "PATH=$PATH" >&2
command -v shellcheck node python3 >&2Fixes, from most to least robust:
- Check for required tools at the top of the hook and fail with a clear message.
- Call tools through the project (for example
./node_modules/.bin/eslint) instead of relying on PATH. - Configure the IDE to launch with the login environment.
A hook that handles file names and missing tools
The loop in the earlier shellcheck example splits file names on whitespace. This version uses NUL-separated names and checks that the tool exists. It needs Bash because read -d '' and process substitution are not POSIX.
#!/usr/bin/env bash
# .githooks/pre-commit
# Run shellcheck on the staged version of each added, copied or modified .sh file.
set -u
if ! command -v shellcheck >/dev/null 2>&1; then
echo "pre-commit: shellcheck is not installed; install it or use --no-verify." >&2
exit 1
fi
status=0
while IFS= read -r -d '' f; do
if ! git show ":$f" | shellcheck -; then
status=1
fi
done < <(git diff --cached --name-only --diff-filter=ACM -z -- '*.sh')
exit $statusThe -z option makes Git separate paths with NUL bytes and stops it from quoting unusual characters, so a file named my script.sh works.
Cross-platform pitfalls
A hook that works on your Linux laptop can fail on a teammate's Windows or macOS machine. These are the usual causes.
| Symptom | Likely cause | Fix |
|---|---|---|
| `bad interpreter: /bin/sh^M` or `env: 'bash\r'` | The script has CRLF line endings | Add a `.gitattributes` rule and re-checkout the file |
| Hook ignored on Windows after clone | Executable bit not recorded in the index | `git update-index --chmod=+x` |
| `[[: not found` or `syntax error` | Bash syntax in a script run by `sh` (often dash on Debian and Ubuntu) | Use `#!/usr/bin/env bash`, or write POSIX `sh` |
| `grep: invalid option -- P`, or `sed -i` errors | macOS ships BSD tools; Linux ships GNU tools | Stick to options both support, or install GNU tools explicitly |
| `python3` opens an app store on Windows | The command resolves to a stub | Use the project's own interpreter path |
| Paths look wrong inside a hook | Windows Git versus WSL path styles | Run Git and the hook tools in the same environment |
Line endings
Windows editors and core.autocrlf=true can turn LF into CRLF in a checked-out script. The kernel then looks for an interpreter named /bin/sh followed by a carriage return. Force LF for hooks in the repository:
# .gitattributes
.githooks/* text eol=lf
*.sh text eol=lfAfter adding the rule, run git add --renormalize . and commit. People who already have bad copies need to check the files out again. To check a file, run file .githooks/pre-commit, which reports with CRLF line terminators when the problem is present.
Executable bit on Windows
Windows file systems do not track Unix permissions, so chmod +x has no lasting effect there. Git tracks the mode in the index instead. Record it explicitly and check it:
git update-index --chmod=+x .githooks/pre-commit
git ls-files --stage .githooksThe listing should show mode 100755 for each hook. Mode 100644 means the file will be checked out as non-executable on systems that honor the bit. Git for Windows runs hooks with its bundled shell and reads the shebang itself, so the check matters most for the people who clone on Linux or macOS.
Shell dialects
/bin/sh is not the same program everywhere. On Debian and Ubuntu it is dash, which has no [[ ]], no arrays and no local guarantees beyond POSIX. On macOS it behaves like a minimal Bash. If a hook uses Bash features, say so in the shebang. If you want maximum portability, write POSIX sh and test it with shellcheck --shell=sh.
A short debugging routine
When a report comes in that a hook is broken, follow the same order every time:
- Run
GIT_TRACE=1 git commitand check whether the hook starts. - If it does not start, check
core.hooksPath, the file name, the mode ingit ls-files --stage, and whether the command bypasses hooks. - If it starts and fails with an interpreter error, check line endings and the shebang.
- If it starts and gives a wrong result, run it by hand with
sh -x, then check the exit status, PATH and tool versions. - Ask the person with the problem for the output of
git --versionanduname -a(or the Windows version). Hook behavior differs between Git versions and platforms.
Hooks run on the developer's machine and can be skipped, so a hook that fails to run is not a security failure. CI should still run the same checks, as the earlier chapter on bypassing hooks explained.
Exercise
A teammate on Windows clones the repository and reports that the pre-commit hook in .githooks never runs, though git config core.hooksPath prints .githooks. GIT_TRACE=1 git commit shows no run_command line for the hook. Name two things to check and the command for each.
Show answer
First, check the mode recorded in the index with git ls-files --stage .githooks. If it shows 100644, fix it with git update-index --chmod=+x .githooks/pre-commit, commit, and have the teammate pull. Second, check the file name and line endings: confirm the file is called exactly pre-commit (ls .githooks) and run file .githooks/pre-commit to see whether it has CRLF endings, then add .githooks/* text eol=lf to .gitattributes. Checking git config --show-origin --get core.hooksPath also confirms that no other config file overrides the setting.
Exercise
A hook runs eslint $(git diff --cached --name-only) | tee lint.log and always lets the commit through, even when eslint reports errors. Why, and how would you fix it?
Show answer
In a pipeline, the exit status is that of the last command, tee, which succeeds. The eslint failure is lost. Fix it by not piping, by saving eslint's status explicitly (run eslint with output redirected to the file, then exit with its status), or by using Bash with set -o pipefail. The script should also end with an explicit exit of the saved status.