Claude Code hooks: block pushes, deletes and secrets

Three guards Claude Code cannot talk past: pushes to live branches, file deletes and secret files.

.claude/settings.json
  1. 1Push guardgit push origin mainBlockedfeature-x goes through
  2. 2Delete guardrm -rf ./old-filesAsks you firsthello.py goes through
  3. 3Secrets guardAdd TEST=1 to .envBlockedREADME.md goes through
  1. Three hooks in the project's .claude/settings.json. Claude Code runs them every time, whatever Claude thinks.
  2. 1, Push guard: git push origin main is blocked. A push to feature-x goes through.
  3. 2, Delete guard: a script test.sh with rm -rf ./old-files gets a permission question first. A hello.py with no delete command goes through.
  4. 3, Secrets guard: adding TEST=1 to .env is blocked. An edit to README.md goes through.
10 min per guardNo code

You need

  • Claude Code
  • A project folder to protect
  • A dummy .env file with a made-up line, for the test

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.

Prompt 1: push guard
Add a "push guard" hook to this project. What it must do: stop any git push to the live branches main, master or production. This includes "git push origin main", "git push origin HEAD:main", "git push --force origin main", and a plain "git push" while the branch I am on is one of those. Pushes to any other branch must go through untouched. How to build it: - Use a PreToolUse hook on the Bash tool, registered in this project's .claude/settings.json. - Put the script in .claude/hooks/ and make it read the command from the hook input. Block by exiting with code 2 and a one-sentence reason on stderr, so you (Claude) see why it was blocked. - Use a scripting language that is already installed on this computer. Check first. Install nothing. - Keep it short and put a comment at the top saying what it blocks. When done: show me the one command I can type to prove it fires, and the exact words I should see when it is blocked.
934 characters

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.

Prompt 2: delete guard
Add a "delete guard" hook to this project. What it must do: before you write or edit a script file (.sh, .ps1, .bat, .cmd, .js, .py) whose new content contains a command that deletes files or folders (for example rm, rmdir, del, Remove-Item, unlink, rmtree, fs.rm), stop and ask me to approve it first. Script files with no delete command must go through without a question. Other file types are ignored. How to build it: - Use a PreToolUse hook on the Write and Edit tools, registered in this project's .claude/settings.json. - Put the script in .claude/hooks/. It reads the hook input, checks the file name and the new content, and when it finds a delete command it answers with a permission decision of "ask" plus a short reason. - Use a scripting language that is already installed on this computer. Check first. Install nothing. - Keep it short and put a comment at the top saying what it checks. When done: show me one sentence I can give you to prove it fires, and what I should see.
993 characters

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.

Prompt 3: secrets guard
Add a "secrets guard" hook to this project. What it must do: stop you (Claude) from reading, editing or writing secret files. Block any file whose name is .env or starts with .env. (for example .env.local or .env.production), or ends in .pem, .key, .p12 or .pfx, or is an SSH private key (id_rsa, id_ed25519), or is credentials.json. Template files named .env.example, .env.sample or .env.template are fine and must go through. Every other file must go through untouched. How to build it: - Use a PreToolUse hook on the Read, Edit, Write and MultiEdit tools, registered in this project's .claude/settings.json. - Put the script in .claude/hooks/. It reads the file path from the hook input and checks only the file name, not the folder. Block by exiting with code 2 and a one-sentence reason on stderr that tells you to ask me to change the file by hand. - If the hook input cannot be read, let the call through. Do not crash. - Use a scripting language that is already installed on this computer. Check first. Install nothing. - Keep it short and put a comment at the top saying what it blocks. When done: show me one sentence I can give you to prove it fires, and the exact words I should see when it is blocked. Tell me which file types it blocks and which it lets through.
1,279 characters

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.

Example: a finished secrets guard (Node)
secrets-guard.js
// 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.

  1. Run the test line under each prompt in a fresh Claude Code session.

  2. Match what you see to the words listed under "You should see".

  3. If you see nothing, type /hooks in Claude Code to check the hook is listed. Then restart Claude Code and try again.

  4. Run the "should go through" case too. A guard that blocks everything is as broken as one that blocks nothing.

  5. Read each script once. It should be short, with a comment at the top saying what it blocks.

Common mistakes

  1. Testing with a real .env. Use a dummy file with a made-up line.

  2. Never running the test. Break each guard once, on purpose.

  3. Blocking everything. Always run the case that should go through, such as a feature branch or README.md.

  4. Trusting a script you have not read. Ask Claude to explain it in plain words.

  5. 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

  1. Paste the push guard prompt into Claude Code and run its test.

  2. Add the delete guard and test it.

  3. Make a dummy .env, add the secrets guard and test it.

  4. Commit .claude/settings.json so 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