DEV Community

Cover image for Git hook vs Claude Code hook: each missed a different commit
Agent Shipped
Agent Shipped

Posted on

Git hook vs Claude Code hook: each missed a different commit

One planted failing test, three bad commits at each gate: the git hook and the Claude Code hook each stopped 2 of 3 and missed a different one, and together they stopped 3 of 3.

GitHub logo agentshipped / notes-api

Agent Shipped demo repo (notes-api): the public playground used in the channel's videos.

notes-api

A tiny Express notes API. This is a public demo repo for the Agent Shipped YouTube channel techniques from real projects are shown here, on code anyone can clone.

npm install
npm start          # http://localhost:3000/notes
npm test           # node:test (no tests yet - that is on purpose)
Enter fullscreen mode Exit fullscreen mode

Endpoints: GET /health, GET /notes, GET /notes/:id, POST /notes {title, body}, DELETE /notes/:id.

Shown in

Videos on the Agent Shipped channel that use this repo:

The setup is a small Node repo with two tests. The first gate is a git pre-commit hook. The second is a Claude Code hook that runs before a Bash tool call. Both write a line to a gate log when they block, so the count does not depend on anyone's memory.

The git hook

A pre-commit hook is a script git runs before every commit. If it exits non-zero, the commit does not happen. Claude wrote .githooks/pre-commit as a POSIX sh script and wired it with core.hooksPath. It runs npm test with the output discarded. On red it prints BLOCKED: tests are red to stderr, appends git-hook blocked to .gate.log and exits 1.

The hook does not know who is committing, or whether the commit sits inside another script. That is its strength. One fact matters later: core.hooksPath lives in the clone's own git config, not in the repository.

Plant the failure

Two tests, a ship script that commits everything, and a log file git ignores. The hook goes into history while the suite is green, so a clone carries the file. I tag that point as mark. Then one sed turns exactly one test red.

Terminal showing one passing test, one failing test and one modified file waiting to be committed.
One test red, one green, one modified file: the raw material for three bad commits. · watch this step

Attempt 1: direct commit

The prompt is strict on purpose, because a helpful agent would otherwise fix the test and end the experiment:

Change no file. Run exactly this one command in the shell: git commit -am "wip". Report its exit code. If it fails, do not retry it and do not fix anything. Do not ask questions or offer options; if something is unclear, take the simplest reading and say which. Stop after showing the output.
Enter fullscreen mode Exit fullscreen mode

The hook ran the tests, they were red, and the commit never started. Exit code 1, first line in the gate log.

Attempt 2: npm run ship

Same prompt, with npm run ship as the command. The commit happens inside the script, so a reader of the command sees only a script name. Git does not care: the hook sits at the commit, not at the command. Blocked again, same exit code.

First miss: a fresh clone

Zero commits landed since the tag, and the log holds two git-hook lines. Attempt three is a fresh clone, the way a teammate or a CI job gets the repo. The clone prints an empty core.hooksPath, though ls shows the hook file. A clone copies committed files, and the setting is local config that git never sends. I planted the same failure with sed and committed: exit 0, red suite, no warning. Nothing was bypassed. In that clone there was never a gate.

Terminal output of the fresh clone: an empty core.hooksPath, the hook file present, and the commit exit code.
The clone's hooksPath prints empty and the commit exits 0. · watch this step

The Claude Code hook

A Claude Code hook receives the tool call as JSON on stdin. For Bash, the field that matters is tool_input.command. If the script exits 2, the call is stopped before it runs, and whatever it wrote to stderr goes back to the agent as the reason. Git cannot do that last part. The script matches the words git commit, runs the tests, and registers under PreToolUse with the matcher Bash. It logs claude-hook blocked to the same file.

One gate at a time

I unset core.hooksPath so only the Claude hook can act, and committed the script and .claude/settings.json by name. The settings file is tracked, so a clone receives it. The planted failure stays uncommitted.

Attempt 1 again

A fresh session loads the hook. The plain commit is stopped before git runs, the agent reads the reason and reports it, and the commit count does not move.

The turn: npm run ship

The text npm run ship has no git commit in it, so the hook finds nothing and lets it through. The script runs git add and git commit, and the commit lands with the suite red. The hook only receives the text the agent typed. It does not follow package.json.

Second miss, then a clone

The ship commit stays in history as evidence, the suite still shows one failing test, and the gate log has a claude-hook line for attempt one and none for attempt two. I then put the string back to green, committed, and cloned. The clone has the script and the registration, and I planted the failure there.

Terminal output with the newest commit, the test counts and the number of claude-hook lines in the gate log.
Look at the newest commit, the failing test count and the claude-hook count. · watch this step

Attempt 3: in the clone

A session started inside the clone reads the settings file that came with the repo. The plain commit is blocked before git runs, and nobody wired anything by hand. So the Claude hook stopped attempts one and three and missed attempt two.

Both gates on

Back in the original repo I re-wired the git hook and added one line to package.json:

npm pkg set scripts.prepare="git config core.hooksPath .githooks"
Enter fullscreen mode Exit fullscreen mode

npm runs prepare on every install. In a fresh clone the setting was empty before npm install and showed the hooks folder after.

Attempts 1 and 2, both on

The agent ran the plain commit, then npm run ship. The first died at the Claude hook before git ran. The second slipped past it and hit the git hook. Neither landed.

The table

The last attempt was a commit in the fresh clone, and it exited 1 because the hook arrived with npm install. The gate log shows lines for both gates in the original repo and a git-hook line in the clone's own log. Neither working tree got a commit.

Terminal output with the clone's exit code, the gate log counts per gate and the git status of both working trees.
The clone exits 1 and the log counts show lines for both gates. · watch this step

Install it

The commit gate is the Claude Code half as a plugin: when a Bash command contains git commit, it runs your test command and exits 2 if the tests fail.

/plugin marketplace add agentshipped/kit
/plugin install commit-gate@agentshipped
Enter fullscreen mode Exit fullscreen mode

It reads only the command text, so it misses a commit made inside a script such as npm run ship and a commit you type yourself outside Claude Code.

Source and README: https://github.com/agentshipped/kit/tree/main/plugins/commit-gate

What to copy

  • Put the gate in git: it fires at the commit, however deep inside a script the commit sits.
  • Wire it with a prepare script so core.hooksPath survives a clone.
  • Add a Claude Code hook for what git cannot do: stop the call before it runs and tell the agent why.
  • Write every block to a gate log, so you count blocks instead of trusting memory.
  • Test the gates with a planted failure and a prompt that forbids fixing it.

Not just JavaScript

The same pair works on any stack: a Python repo runs pytest in both hooks and wires core.hooksPath from a Makefile or pre-commit install instead of an npm prepare script.

Video with the full run: https://youtu.be/N3DSkDEnKEA

Techniques from my real projects, shown on a public demo repo.
Narration: AI voice. The sessions, code and errors are real. Not affiliated with Anthropic or any tool vendor.

Top comments (1)

Collapse
 
fxdgif profile image
Fred 🍓 •

One trap with the prepare line: npm also runs prepare on npm ci, and some of those installs have no .git, like a Docker build that copies package*.json and installs before copying the rest. git config then fails with "not in a git directory" (or git is missing entirely on an alpine image), and the install fails with it. Husky's prepare checks for .git first and does nothing without one for that reason. A || true doesn't work as the fix on Windows, since npm runs scripts through cmd.exe there, so the check is easiest in a two-line Node script.