Your rules for Claude are advice. Claude reads them, then decides. Sooner or later it pushes to the wrong branch, writes a script that deletes files, or opens the file with your passwords in it.
A hook is a small script that Claude Code runs at a set moment, every time, whatever Claude thinks. Your rule is a "staff only" sign on a door. A hook is the lock.
This guide gives you three prompts. Each one tells Claude Code, in plain English, to build one guard for you. You write no code, and each prompt ends with a test so you can watch the lock work.
01 / 06
What a hook is
Claude Code can run a script just before it uses a tool, such as running a command or editing a file. That script can say no. When a hook says no, the tool call stops, and Claude is shown the reason.
Claude Code's own hooks guide says hooks give "deterministic control": certain actions always happen, instead of relying on the model to choose to run them. As of November 2026, that is how they work.
Two details to know:
- Hooks live in your project's settings file,
.claude/settings.json. Commit it and your whole team gets the same locks. - A hook runs on your computer with your permissions. Read the script
Claude writes before you rely on it.
Each prompt asks Claude to use a scripting language already installed on your computer, and to install nothing.
02 / 06
1. Push guard
The push guard stops any push to the live branches: main, master or production. Pushes to any other branch go through. It catches the usual ways of typing a push, not every one, so treat it as a guard rail, not a lock. Claude Code also asks your OK before it writes files into
.claude, so say yes when it does.
Test it once. Start a fresh Claude Code session in the project and ask: "Run
git push origin main."
You should see: Claude reports the command was blocked, with a reason like: Blocked: push targets protected branch "main". Then ask it to push a branch called
feature-x. That one should go through.
03 / 06
2. Delete guard
The delete guard works on scripts. Before Claude writes or edits a script file that deletes files or folders, you get a yes or no question first. Scripts with no delete command go through without a question.
Test it once. Ask: "Create a file called test.sh with the content rm -rf ./old-files."
You should see: a permission question before the file is written, with a reason that mentions a delete command. Then ask for a hello.py that only prints "hello". That one should be written with no question.
04 / 06
3. Secrets guard
The secrets guard blocks Claude from reading or editing the
.env file, where your passwords and keys usually live. It also blocks files that end in .pem, .key, .p12 or .pfx, SSH private keys, and credentials.json. Template files such as .env.example go through, because they hold no real secrets.
Before you test this one, make a dummy file called .env in your project root with one made-up line, such as FAKE_KEY=not-a-real-key. Never test with a real .env.
Test it once. Ask: "Add a line TEST=1 to my .env file."
You should see: Claude reports the edit was blocked, with a reason like: Blocked: ".env" is a secret file. Ask the user to change it by hand. Then ask it to add a line to
README.md. That one should go through.
Here is what a finished secrets guard can look like. Yours may differ, and that is fine. The shape to look for is the same: it reads the file name, and it exits with code 2 to block.
// Secrets guard: blocks Claude Code from reading, editing or writing secret files // (.env and .env.*, *.pem, *.key, *.p12, *.pfx, SSH private keys, credentials.json). // Template files such as .env.example are allowed. Blocks by exiting with code 2. let raw = ""; process.stdin.on("data", (c) => (raw += c)); process.stdin.on("end", () => { let input = {}; try { input = JSON.parse(raw); } catch { process.exit(0); } const ti = input.tool_input || {}; const file = ti.file_path || ti.notebook_path || ""; if (!file) process.exit(0); const name = file.replace(/\\/g, "/").split("/").pop().toLowerCase(); const isTemplate = /\.(example|sample|template|dist)$/.test(name); const secret = ((name === ".env" || name.startsWith(".env.")) && !isTemplate) || /\.(pem|key|p12|pfx|keystore)$/.test(name) || /^id_(rsa|dsa|ecdsa|ed25519)$/.test(name) || name === "credentials.json"; if (secret) { process.stderr.write(`Blocked: "${name}" is a secret file. Ask the user to change it by hand.\n`); process.exit(2); } process.exit(0); });
When we fed this script a sample edit of .env, it stopped it with exit code 2 and the message above. A sample edit of README.md went through. The guard watches Claude's file tools. It does not watch shell commands, so
cat .env through the Bash tool is a separate door. To close it, ask Claude to extend the hook to the Bash tool and block commands that name a secret file.
05 / 06
Test it safely
A hook you have never seen fire is a hook you cannot trust. So break each guard once on purpose.
Run the test line under each prompt in a fresh
Claude Code session.
Match what you see to the words listed under "You should see".
If you see nothing, type
/hooksinClaude Code to check the hook is listed. Then restart Claude Code and try again.
Run the "should go through" case too. A guard that blocks everything is as broken as one that blocks nothing.
Read each script once. It should be short, with a comment at the top saying what it blocks.
Common mistakes
Testing with a real .env. Use a dummy file with a made-up line.
Never running the test. Break each guard once, on purpose.
Blocking everything. Always run the case that should go through, such as a feature branch or
README.md.Trusting a script you have not read. Ask
Claude to explain it in plain words.
Treating the guards as complete. They cover the doors the prompts name. Add more guards as you find other doors.
What is a hook, in one sentence?
A small script Claude Code runs at a set moment, every time, whatever Claude thinks. It can stop an action and tell Claude why.
I ran the test and nothing was blocked.
Type /hooks to see whether the hook is listed. Restart Claude Code, then run the test again in a fresh session.
Does the secrets guard stop Claude seeing my passwords?
It stops Claude's file tools from reading or editing secret files. A shell command such as cat .env is a separate door, so ask Claude to extend the hook to the Bash tool as well.
Can my team use the same guards?
Yes. Hooks live in .claude/settings.json in the project. Commit it and everyone who opens the project gets the same locks.
Finish line
What to do next
Paste the push guard prompt into
Claude Code and run its test.
Add the delete guard and test it.
Make a dummy
.env, add the secrets guard and test it.Commit
.claude/settings.jsonso the locks stay in the project.
For more ways to keep Claude's access small, see AI guardrails that hold and give your AI its own login. More setup guides are in the guides hub.
You reached the end of the guide