Claude Code hooks starter: block risky commands and protect files

Three copy-paste Claude Code hooks: block edits to .env and lockfiles, block git push and rm -rf, and auto-format with Prettier. Tested.

· By Codema Editorial

Free account required

CLAUDE.md is advice. Hooks always run. When a hook exits with code 2, Claude Code blocks the action.

Preview: block-risky-bash.sh

#!/bin/bash
# PreToolUse hook (Bash): blocks commands you never want an agent to run alone.
# From codema.ai. Exit code 2 blocks the command and sends stderr to Claude.
# This is a safety net, not a sandbox: a determined rewrite can slip past a pattern.
COMMAND=$(jq -r '.tool_input.command // empty')
RISKY_PATTERNS=("rm -rf" "git push" "git reset --hard" "supabase db reset" "vercel --prod" "DROP TABLE" "drop table")
for pattern in "${RISKY_PATTERNS[@]}"; do
  if [[ "$COMMAND" == *"$pattern"* ]]; then
    echo "Blocked: command contains '$pattern'. Ask the user to run it themselves." >&2
    exit 2
  fi
done
exit 0

What's inside

  • protect-files.sh: no edits to .env, lockfiles or .git/
  • block-risky-bash.sh: no rm -rf, git push, git reset --hard, supabase db reset, vercel --prod, DROP TABLE
  • A Prettier hook that formats after each edit
  • .claude/settings.json that registers all three

How to use it

  1. Unzip .claude/ into your project. Merge hooks if you already have a settings file.
  2. Install jq.
  3. Run chmod +x .claude/hooks/*.sh.
  4. Type /hooks in Claude Code to check.
  5. Test: ask Claude to edit .env. It should be blocked.

Who it's for

Claude Code users who let the agent run commands on its own.

Keep reading

Keep reading

Newsletter

The Codema publication. Consent is required. You are not subscribed until you confirm from the email, and nothing is stored if email delivery is not connected.