NativeAIHub
🛡️

Pre-Commit Check Hook

Validates your code before Claude commits, running type checks, tests, or custom scripts to prevent broken commits.

Hook·2 sections·1 min read
codingdevops
Install Prompt

Paste this into Claude Code to set it up:

Add a PreToolUse hook to your ~/.claude/settings.json that intercepts Bash tool calls containing "git commit" and runs validation first.

In your settings.json, add this under the "hooks" key:

"PreToolUse": [
  {
    "matcher": "Bash",
    "command": "echo $CLAUDE_TOOL_INPUT | grep -q 'git commit' && npx tsc --noEmit 2>&1 || true"
  }
]

This checks for TypeScript type errors before any commit goes through. If tsc finds errors, Claude sees them and can fix the issues before committing. Replace tsc with your preferred validation command (jest, vitest, a custom script) depending on your project.

01What It Does

A safety net before every commit

The Pre-Commit Check Hook intercepts Bash tool calls that contain git commit and runs your validation suite before the commit executes. If type checking fails or tests break, Claude sees the errors and can fix them before trying again. This is your last line of defense against committing code that does not compile or pass tests.
1

Claude initiates a commit

When Claude runs a Bash command containing <code>git commit</code>, the PreToolUse event fires before the command executes.

2

Hook detects the commit intent

The command inspects <code>$CLAUDE_TOOL_INPUT</code> for the string "git commit". If found, it triggers the validation step. Other Bash commands pass through unaffected.

3

Validation runs

Your configured checks execute: type checking with tsc, test suite with jest or vitest, or a custom validation script. Any failures produce output that Claude can read.

4

Claude responds to the result

If validation passes, the commit proceeds normally. If it fails, Claude sees the errors, fixes the code, and tries the commit again with clean code.

02Validation Strategies

🔷
Type CheckingRun <code>tsc --noEmit</code> to catch type errors without generating output files. Fast, reliable, and catches a large class of bugs that would otherwise slip through. Best for TypeScript projects where type safety is a priority.
🧪
Test SuiteRun <code>jest --bail</code> or <code>vitest run</code> to execute your test suite before committing. The <code>--bail</code> flag stops at the first failure for faster feedback. Best for projects with good test coverage where passing tests are a hard requirement.
📜
Custom ScriptPoint the hook at a script like <code>~/.claude/hooks/pre-commit.sh</code> that runs multiple checks in sequence: type checking, linting, tests, and anything else your project requires. Best for teams with specific quality gates that go beyond standard tooling.