Git guardrails for Claude Code
Installs a Claude Code PreToolUse hook that stops the usual forms of git push, reset --hard, clean -f, branch -D and checkout or restore of the whole tree, for one project or all, then tests it. Use when you want a safety net against an agent pushing or discarding work on its own.
git-guardrails-claude-code- Author
- Matt Pocock
- Source
- mattpocock/skills/skills/misc/git-guardrails-claude-code @ abeeaf9
- License
- MIT · Copyright (c) 2026 Matt Pocock
About this skill
What it does
The skill installs a safety net in Claude Code: a PreToolUse hook that searches the text of every Bash command for patterns from a list before it runs and stops the command on a match. The default list has git push (--force included), git reset --hard, git clean -f and -fd, git branch -D, git checkout . and git restore .. The agent gets exit code 2 and a message saying the user has prevented this.
In five steps the agent asks whether to protect this project (.claude/settings.json) or all projects (~/.claude/settings.json), copies the script, merges the hook into hooks.PreToolUse without touching other settings, asks which patterns to add or remove, and tests the script with a fake git push.
The folder ships scripts/block-dangerous-git.sh, the hook itself: it pulls the command out of the tool call with jq and looks for the patterns as substrings. It does not parse the command, so the same operation written another way gets through: git clean -df, git clean -xfd, git -C . push origin main, git checkout -- ., git branch --delete --force x. Without jq the script cannot read the command and silently lets everything through. agents/openai.yaml holds the name and short description for Codex.
When to use it, and when not
Use it when Claude Code runs unattended and you want a guard against the usual destructive git commands that holds better than a line in the instructions. It is a safety net, not a guarantee: a command written differently slips past it. The default list leaves history rewrites alone: git rebase and git commit --amend run unless you add them in step four.
Change the defaults if the agent should push its own branches: the git push pattern stops a plain push too. The hook goes into Claude Code's settings. Codex does not read them, so there is no block there; Cursor can pick up Claude Code hooks through its import of third-party settings, but check that the block actually fires there. The script needs bash and jq. Patterns match anywhere in the command line, so git checkout .env is blocked too, and so is a commit whose message mentions "git push".
How to run it
The install command below puts the whole folder in place. The agent picks the skill up by its description when you ask to block dangerous git commands; to call it yourself:
- Claude Code:
/git-guardrails-claude-code, or ask for the skill by name. - Codex:
$git-guardrails-claude-code. - Cursor: type
/in the Agent chat and pickgit-guardrails-claude-code.
Example: /git-guardrails-claude-code Install it for all my projects and block git rebase as well.
How it differs from similar skills
Safe git recovery works the other end: it recovers work through the reflog after a reset or a lost branch. This skill tries to prevent the loss by stopping the usual dangerous commands before they run, but a command in an unusual form gets through, and then recovery is what helps. Git workflow and versioning suggests git reset --hard HEAD to return to the last good state; the default list blocks it, so pick one approach. Pre-commit hooks setup installs git hooks that check commits; this hook checks the agent's commands. Guardrails for destructive Docker commands covers the same risk for Docker, but as rules for the agent rather than a hook: it states what a command would delete and waits for your confirmation.
Install
Run one command — it saves SKILL.md and the files it uses into your agent's skills folder. Claude Code and Codex pick up a new skill without a restart; if it does not show up, restart the agent.
curl -fsSL --create-dirs -o ~/.claude/skills/git-guardrails-claude-code/SKILL.md https://anymodel.org/en/skills/matt-pocock/git-guardrails-claude-code/SKILL.md -o ~/.claude/skills/git-guardrails-claude-code/LICENSE https://anymodel.org/en/skills/matt-pocock/git-guardrails-claude-code/LICENSE -o ~/.claude/skills/git-guardrails-claude-code/agents/openai.yaml https://anymodel.org/en/skills/matt-pocock/git-guardrails-claude-code/agents/openai.yaml -o ~/.claude/skills/git-guardrails-claude-code/scripts/block-dangerous-git.sh https://anymodel.org/en/skills/matt-pocock/git-guardrails-claude-code/scripts/block-dangerous-git.sh && chmod +x ~/.claude/skills/git-guardrails-claude-code/scripts/block-dangerous-git.sh
curl -fsSL --create-dirs -o ~/.agents/skills/git-guardrails-claude-code/SKILL.md https://anymodel.org/en/skills/matt-pocock/git-guardrails-claude-code/SKILL.md -o ~/.agents/skills/git-guardrails-claude-code/LICENSE https://anymodel.org/en/skills/matt-pocock/git-guardrails-claude-code/LICENSE -o ~/.agents/skills/git-guardrails-claude-code/agents/openai.yaml https://anymodel.org/en/skills/matt-pocock/git-guardrails-claude-code/agents/openai.yaml -o ~/.agents/skills/git-guardrails-claude-code/scripts/block-dangerous-git.sh https://anymodel.org/en/skills/matt-pocock/git-guardrails-claude-code/scripts/block-dangerous-git.sh && chmod +x ~/.agents/skills/git-guardrails-claude-code/scripts/block-dangerous-git.sh
foreach ($f in 'SKILL.md','LICENSE','agents/openai.yaml','scripts/block-dangerous-git.sh') { $p = Join-Path "$HOME\.claude\skills\git-guardrails-claude-code" $f; New-Item -ItemType Directory -Force (Split-Path $p) | Out-Null; Invoke-WebRequest "https://anymodel.org/en/skills/matt-pocock/git-guardrails-claude-code/$f" -UseBasicParsing -OutFile $p }foreach ($f in 'SKILL.md','LICENSE','agents/openai.yaml','scripts/block-dangerous-git.sh') { $p = Join-Path "$HOME\.agents\skills\git-guardrails-claude-code" $f; New-Item -ItemType Directory -Force (Split-Path $p) | Out-Null; Invoke-WebRequest "https://anymodel.org/en/skills/matt-pocock/git-guardrails-claude-code/$f" -UseBasicParsing -OutFile $p }Any other agent: save the file into its skills folder, or give it the link to the file: https://anymodel.org/en/skills/matt-pocock/git-guardrails-claude-code/SKILL.md
SKILL.md
Setup Git Guardrails
Sets up a PreToolUse hook that intercepts and blocks dangerous git commands before Claude executes them.
What Gets Blocked
git push(all variants including--force)git reset --hardgit clean -f/git clean -fdgit branch -Dgit checkout ./git restore .
When blocked, Claude sees a message telling it that it does not have authority to access these commands.
Steps
1. Ask scope
Ask the user: install for this project only (.claude/settings.json) or all projects (~/.claude/settings.json)?
2. Copy the hook script
The bundled script is at: scripts/block-dangerous-git.sh
Copy it to the target location based on scope:
- Project:
.claude/hooks/block-dangerous-git.sh - Global:
~/.claude/hooks/block-dangerous-git.sh
Make it executable with chmod +x.
3. Add hook to settings
Add to the appropriate settings file:
Project (.claude/settings.json):
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-dangerous-git.sh"
}
]
}
]
}
}
Global (~/.claude/settings.json):
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "~/.claude/hooks/block-dangerous-git.sh"
}
]
}
]
}
}
If the settings file already exists, merge the hook into the existing hooks.PreToolUse array. Don't overwrite other settings.
4. Ask about customization
Ask if user wants to add or remove any patterns from the blocked list. Edit the copied script accordingly.
5. Verify
Run a quick test:
echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>
Should exit with code 2 and print a BLOCKED message to stderr.
Connect through AnyModel API
This skill runs on whatever model your agent uses. Point Claude Code, Codex or Cursor at AnyModel to run it on Claude, GPT, Gemini and more with one key — pay per token, no subscription.
AnyModel