Agent Skill
Git guardrails for Claude Code
Matt Pocock's skill that sets up a Claude Code hook to block destructive Git commands such as push, reset --hard, clean and branch -D before they run.
Bookmark
Bookmarks are kept in your free account, so you can find this page again on any device.
Sign in to bookmarkInstall
Commands are pinned to commit abeeaf9, the version we read, wherever the install method supports it.
npx skills
npx skills add mattpocock/skills@abeeaf95660ba9e8b7c753eb38f441ca707a8823 --skill git-guardrails-claude-code -a claude-codeCopies the skill into .claude/skills/ in this project, pinned to the reviewed commit.
Plugin marketplace
/plugin install mattpocock-skills@claude-plugins-officialRun inside Claude Code. Installs the whole mattpocock-skills plugin from its default branch, not the pinned commit; the official marketplace is available by default.
npx skills
npx skills add mattpocock/skills@abeeaf95660ba9e8b7c753eb38f441ca707a8823 --skill git-guardrails-claude-codePinned to the commit we reviewed. The CLI asks which agents to install for; add -g to install for your user instead of this project.
What it does
A setup skill rather than a working-style skill. It asks whether to install at project or user scope, copies scripts/block-dangerous-git.sh into place, adds a PreToolUse hook to the matching Claude Code settings file, offers to customise the blocked list and verifies the hook works.
When to use it
Claude Code users who run long or unattended sessions and want a hard stop on git push --force, git reset --hard and similar commands. A hook is enforced by Claude Code itself, so it holds even when the model ignores an instruction.
It is also a good small example of a setup skill: the skill installs something (a script and a hook) and then gets out of the way, rather than shaping how the agent works.
When to skip it
Other agents: the hook format is Claude Code’s. And if you only need to block a couple of commands, Claude Code’s permission deny rules do it without a script.
Try it
Set up git guardrails for this project only. Block force pushes,
hard resets and branch deletion, then show me a blocked command to prove
the hook works.
What it can touch
- Bundles scripts
- Yes
- Pre-approved tools
- None declared
- Needs
- Claude Code; jq (used by the hook script)
- Client-specific fields
- None, spec fields only
claude.ai and the Claude API reject uploads that use fields outside the open spec or names containing "claude" or "anthropic", so this skill needs editing before you upload it there.
Its scripts run with your permissions, and it may have changed since the commit we read. How to check a skill.
Read the shell script; it is short. The hook only applies to Claude Code. The skill name contains “claude”, which claude.ai and the Skills API reserve, so it cannot be uploaded there.
At commit abeeaf9 the SKILL.md is 96 lines long. Alongside it:
agents/(1 entry)scripts/(1 entry)
Alternatives
Claude Code’s own permission rules, described in our Agent Skills in Claude Code guide. Finishing a development branch handles merges and pushes deliberately at the end of a task.
SKILL.md
The file exactly as published at the reviewed commit, reproduced under its MIT licence.
---
name: git-guardrails-claude-code
description: Set up Claude Code hooks to block dangerous git commands (push, reset --hard, clean, branch -D, etc.) before they execute. Use when user wants to prevent destructive git operations, add git safety hooks, or block git push/reset in Claude Code.
---
# 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 --hard`
- `git clean -f` / `git clean -fd`
- `git branch -D`
- `git 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](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`):
```json
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-dangerous-git.sh"
}
]
}
]
}
}
```
**Global** (`~/.claude/settings.json`):
```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:
```bash
echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>
```
Should exit with code 2 and print a BLOCKED message to stderr.Related skills
Devcontainer setup (Trail of Bits)
Writes a .devcontainer/ for your project with Claude Code, Python, Node, Rust or Go tooling and persistent volumes, so an agent can work inside a container.
Skill creator
Anthropic's skill for writing new skills and improving old ones: interviews you, drafts SKILL.md, runs evals against a baseline and tunes the description.
CLAUDE.md improver (Anthropic)
Anthropic's Claude Code skill that finds every CLAUDE.md in a repo, grades each one A to F, shows a report, then edits only the files you approve.
Guides and recipes that use Git guardrails for Claude Code
- Set up guardrail hooks for your coding agent
Block dangerous commands in Claude Code with PreToolUse hooks and deny rules, so the agent cannot force-push, wipe files or edit secrets. Test them first.
- Skills vs subagents vs slash commands vs hooks
Skills vs subagents, slash commands, hooks, MCP servers and AGENTS.md: when to reach for each in a coding agent, with a decision table and a worked example.
- Agent Skill best practices: nine design patterns with real examples
Agent skill best practices as nine design patterns, from checklists and routers to script-backed and gated skills, each with a real example and when to use it.
- Claude Code skills: folders, invocation and plugins
Where Claude Code skills live, which copy wins when names clash, how /skill-name works, Claude Code's extra frontmatter fields and how plugin skills are named.
- Skill recipe: turn an incident runbook into a skill
A worked runbook skill for one service: a triage checklist, a read-only diagnostics script, and fixes the agent may only propose until a person says yes.
- Claude Code
Anthropic's agentic coding tool for the terminal, IDEs, desktop and web.