liveBook · Manning

Git hook

As explained in Vibe Engineering

A Git hook is a script automatically executed by Git in response to a repository event such as committing code.

A Git hook runs a program automatically at a repository event. A pre-commit hook runs before a commit is recorded; a pre-push hook runs before changes leave the local machine. These points can put checks close to the changes they review. section 5.3.2

A hook that blocks on an AI reviewer’s judgment can create friction: false positives may teach developers to bypass the hook with --no-verify, bypassing deterministic checks too. A safer split is to let deterministic checks block and let a model advise on questions such as project conventions that are harder to express as fixed rules. section 5.3.2

Listing 5.4 Pre-commit hook example
#!/bin/bash
# .git/hooks/pre-commit
set -euo pipefail
 
DIFF=$(git diff --cached)
[ -z "$DIFF" ] && exit 0
 
# Deterministic checks block. This is the real gate.
gitleaks protect --staged --redact || exit 1
 
# The model advises. It cannot block on its own.
VERDICT=$(printf '%s' "$DIFF" | claude -p "Review this diff against our
conventions: error handling, test coverage for new public functions,
naming. Reply with exactly one line of JSON and nothing else:
{\"verdict\":\"clean\"} or {\"verdict\":\"concerns\",\"notes\":\"...\"}")
 
if [ "$(printf '%s' "$VERDICT" | jq -r '.verdict')" = "concerns" ]; then
    echo "AI reviewer raised concerns (advisory, not blocking):"
    printf '%s' "$VERDICT" | jq -r '.notes'
fi
exit 0

This pre-commit example scans the staged diff with gitleaks, then asks an AI reviewer for a JSON verdict parsed with jq. The model’s concerns are printed as warnings. section 5.3.2

The example’s intended split needs an operational check: because the script uses set -e and pipefail, a failed model command can terminate the hook even though the model is meant to be advisory. Test failure paths as well as ordinary verdicts. Structured parsing also matters: searching for PASS could mistakenly accept a response containing both FAIL and PASSWORD. section 5.3.2

Git hooks are distinct from agent lifecycle hooks, which respond to events inside an agent’s run rather than repository events. Hooks are also an execution boundary: a compromised agent could modify a pre-commit hook that later runs under the developer’s authority, outside the sandbox. section 5.6.5 section 5.6.6

How Vibe Engineering covers it

How deep it goes
Explains hook timing and a pre-commit script, including blocking authority, structured verdicts, and bypass risks.
Examples
Uses a staged-diff hook with gitleaks and a JSON-producing AI reviewer parsed by jq.
What you can do after
Evaluate and understand where to place blocking checks, advisory review, and safeguards around hook execution.
How it connects
Connects Git hooks with secret scanning, linters, AI code review, and sandboxing.

Also explained in

Compare how 3 books explain git hook →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vibe Engineering. The section links point to the parts of the book it draws on.