Git Hook Not Running? Five Reasons It Does Nothing, and How to Tell Which

Date:

You added a pre-commit hook to block a mistake. You make the mistake on purpose to test it, and the commit goes through. No error, no output. Is the hook running at all?

The short answer: a Git hook not running means one of two things: either Git never starts the hook, or it starts it and the check inside fails quietly. Git skips a hook that has the wrong file name, that sits in .git/hooks while core.hooksPath points Git somewhere else, or (on Linux and macOS) that isn’t executable. When Git does start it, a hook that calls another script with 2>/dev/null || true hides every failure of that script, including “file not found”. git rev-parse --git-path hooks tells you which folder Git reads, and sh -x on the hook shows what it really ran.

Git hook not running: which cause is yours?

What you seeLikely causeCheck it in one line
Nothing at all, in a new repoWrong file name (pre-commit.sh, pre-commit.sample)ls .git/hooks
Nothing, though the file looks rightcore.hooksPath sends Git to another foldergit rev-parse --git-path hooks
hint: The '.git/hooks/pre-commit' hook was ignored because it's not set as executableMissing execute permission (Linux, macOS)ls -l .git/hooks/pre-commit
Nothing, and the hook’s folder is the right oneThe hook runs, but the script it calls is missing and the error is silencedsh -x "$(git rev-parse --git-path hooks)/pre-commit"
Nothing, the script existsThe script crashes looking for a file it expects beside it, and the crash is silencedRun the script by hand, without the hook

Step 1: check the file name

Git runs a hook only when the file is named exactly after the hook, with no extension. On a test machine, a hook saved as pre-commit.sh that should refuse every commit let one through:

$ git commit -m "one"
$ ls .git/hooks | grep pre-commit
pre-commit.sample
pre-commit.shCode language: JavaScript (javascript)

Renamed to pre-commit, the same file ran and stopped the commit:

$ git commit -m "two"
pre-commit: checking...Code language: JavaScript (javascript)

The list of hook names is fixed: pre-commit, commit-msg, pre-push and so on.

Step 2: check which folder Git reads

If core.hooksPath is set, in the repo, in your user config or by a tool you installed, Git reads hooks from that folder and ignores .git/hooks completely. Ask Git where it looks, and where the setting came from:

$ git rev-parse --git-path hooks
/home/you/shared-hooks
$ git config --show-origin core.hooksPath
file:/home/you/.gitconfig	/home/you/shared-hooksCode language: JavaScript (javascript)

Put your hook in the folder Git reports, or remove the setting if you didn’t mean to have one. Tools that manage hooks for you (Husky, pre-commit, a team’s shared hooks folder) often set core.hooksPath, which is why a hook that worked in one repo does nothing in another.

Step 3: on Linux and macOS, make it executable

Git skips a hook without execute permission and, by default, prints a hint saying so. Fix it with chmod +x .git/hooks/pre-commit. If the file came from a Windows machine or a zip file, also check that the first line is a valid #! line with Unix line endings. Windows has no execute bit. Git for Windows decides by the file itself, and a shell-script hook needs that #! first line to run.

Step 4: the hook runs, but the check inside it doesn’t

This is the case that stays hidden for months. Hooks are often a short shell script that launches the real check, written so a broken check never blocks anyone’s work:

#!/bin/sh
HOOKDIR=$(dirname "$0")
python "$HOOKDIR/check_secrets.py" 2>/dev/null || true
Code language: Bash (bash)

2>/dev/null throws away the error message and || true throws away the failure. If check_secrets.py was never copied into the hooks folder, every commit goes through and nothing is printed. sh -x shows each command the hook runs:

$ sh -x "$(git rev-parse --git-path hooks)/pre-commit"
++ dirname /home/you/shared-hooks/pre-commit
+ HOOKDIR=/home/you/shared-hooks
+ python /home/you/shared-hooks/check_secrets.py
+ trueCode language: JavaScript (javascript)

The + true line is the giveaway: the check failed and the hook carried on. Run the same line without the silencer to see why:

$ python /home/you/shared-hooks/check_secrets.py
python: can't open file '/home/you/shared-hooks/check_secrets.py': [Errno 2] No such file or directoryCode language: JavaScript (javascript)

A missing check usually means whatever installs the hooks copies the hook but not the scripts it launches. Someone added a check to the hook and forgot the installer.

Step 5: the check is there, but can’t find its own files

The next trap is a check that worked from the project folder and breaks once copied into the hooks folder. This one reads its rules from the folder above its own, which was the project root when it lived in tools/:

rules = json.loads((Path(__file__).resolve().parent.parent / "rules.json").read_text())Code language: Python (python)

From the hooks folder, “one folder up” is somewhere else entirely, and the hook’s silencer hides the crash. Run the check by hand from the hooks folder:

$ python "$(git rev-parse --git-path hooks)/check_secrets.py"
FileNotFoundError: [Errno 2] No such file or directory: '/home/you/rules.json'Code language: JavaScript (javascript)

Install the files the check reads next to it, and have the check look beside itself first.

Make the hook fail loudly

Keep a hook from blocking work when a check is optional, but never let a missing check pass in silence. Test for the script, and say so when it isn’t there:

#!/bin/sh
HOOKDIR=$(dirname "$0")
CHECK="$HOOKDIR/check_secrets.py"
if [ ! -f "$CHECK" ]; then
    echo "pre-commit: $CHECK is missing; the secrets check did not run" >&2
    exit 1
fi
python "$CHECK"
Code language: Bash (bash)
$ git commit -m "six"
pre-commit: /home/you/shared-hooks/check_secrets.py is missing; the secrets check did not runCode language: JavaScript (javascript)

If you’d rather warn than block, print the same message and exit 0. Either way, a missing check now shows up the first time someone commits. If you install hooks with a script, add a test that compares every script the hooks launch with the files the installer copies, so the two can’t drift apart again.

The short version

When a Git hook does nothing, find out whether Git starts it. Check the exact file name, ask git rev-parse --git-path hooks which folder Git reads, and on Linux or macOS check the execute bit. If Git does start it, run the hook with sh -x: a + true after a failed command means a silenced check. Remove the silencer, install every script the hook calls and every file those scripts read, and make the hook report a missing check. If your hooks are slow rather than silent, see why a pre-push hook gets slower every week.

Frequently Asked Questions

How do I run a hook without making a commit?

Run the file directly: sh "$(git rev-parse --git-path hooks)/pre-commit". Add -x to see every command. Hooks that read input, like pre-push, expect data on standard input, so give them an empty input (< /dev/null) to test that they start.

Can I skip a hook on purpose?

Yes. git commit --no-verify and git push --no-verify skip the client-side hooks for that one command. That’s also why a local hook is a reminder rather than a guarantee: a check that must never be skipped belongs in your CI or on the server.

Why does my hook work in one repo and not another?

Usually core.hooksPath. If it is set in your user config, every repo uses that folder; if it is set in one repo’s config, only that repo does. git config --show-origin core.hooksPath in each repo shows the difference.

Should I commit hooks to the repository?

.git/hooks itself is never committed or cloned. To share hooks, commit them to a folder in the repo (such as .githooks/) and have each person run git config core.hooksPath .githooks once, or use a hook manager that does it for them.

Share post:

Subscribe

Popular

More like this
Related

Stop WordPress Content Scraping Through Feeds, the REST API and Load More

Someone copies your whole archive and never crawls a single page. A default WordPress site hands every post's full text to any visitor who asks the right three addresses.

Contact Form 7 Scripts on Every Page? Load Them Only Where a Form Is Shown

Your site has one contact form, yet every page downloads Contact Form 7's stylesheet, two scripts and a block of settings. The plugin has its own switch for that. The catch is the form that isn't in the page content.

Test WordPress SMTP Without Sending a Single Real Email

You changed the SMTP settings, or copied a live site to staging, and you need to know whether mail works. Every test you can think of either lands in a real inbox or, on a copy of a live site, could reach a real customer.

Contact Form 7 Emails Going to Spam? Send Them Through Your Own Mailbox With FluentSMTP

The form said "Thank you for your message. It has been sent." It had been. Straight into the spam folder.