git-guardrails-claude-code — Claude Code skill guide
This skill adds a Claude Code hook that stops risky Git commands before they run. Use it when an agent must not push or discard working-tree changes.
- Author Matt Pocock
- License MIT
- GitHub ★ 302,251
- Installs 499,069
- Updated 2026-10-22
- Source shown below
What git-guardrails-claude-code does
git-guardrails-claude-code adds a PreToolUse hook for Bash. It intercepts git push, including variants with --force, before Claude Code runs it. The hook also blocks git reset --hard, git clean -f and git clean -fd, git branch -D, git checkout ., and git restore ..
The skill asks whether to install for the current project in .claude/settings.json or for all projects in ~/.claude/settings.json. It copies scripts/block-dangerous-git.sh into .claude/hooks or ~/.claude/hooks, makes it executable with chmod +x, then adds it to hooks.PreToolUse. Existing settings are kept and the hook is merged into the current array.
You can edit the copied script to change blocked patterns, then test it with a tool_input.command payload. Install this for teams that want human approval over destructive Git actions; skip it when the agent is intentionally allowed to push or clean the worktree.
What it helps with
- block
git pushbefore an agent runs it - prevent
git reset --hard - stop cleanup through
git clean -f - deny branch deletion with
git branch -D - attach Git command checks to
PreToolUse
How to install git-guardrails-claude-code
Command from the source
/plugin marketplace add mattpocock/skillsCommand from the source
/plugin install mattpocock-skills@mattpocockCommand from the source
npx skills add mattpocock/skills --skill git-guardrails-claude-code -a claude-codeWith the Skills CLI (Vercel)
npx skills add mattpocock/skills --skill git-guardrails-claude-code -a claude-codeManually
~/.claude/skills/git-guardrails-claude-code/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/git-guardrails-claude-code/
Manually
.agents/skills/git-guardrails-claude-code/SKILL.mdFor all projects use ~/.agents/skills/
Manually
.cursor/skills/git-guardrails-claude-code/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
GitHub CLI (gh ≥ 2.90)
gh skill install mattpocock/skills git-guardrails-claude-codeManually
.github/skills/git-guardrails-claude-code/SKILL.mdGemini CLI
gemini skills install https://github.com/mattpocock/skills --path skills/misc/git-guardrails-claude-codeManually
.devin/skills/git-guardrails-claude-code/SKILL.mdThe legacy .windsurf/skills/ path still works
Claude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
How to use it: example prompts
- “Install git-guardrails-claude-code for this project only.”
- “Set up a global hook that blocks `git push`.”
- “Check that the hook rejects `git push origin main`.”
Source text
SKILL.md · MIT
---
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.
© Matt Pocock · MIT · mattpocock/skills
Questions about git-guardrails-claude-code
How do I install git-guardrails-claude-code in Claude Code?
Add the skill with npx skills add mattpocock/skills --skill git-guardrails-claude-code -a claude-code. Then choose .claude/settings.json for one project or ~/.claude/settings.json for all projects.
What does git-guardrails-claude-code do?
It configures a PreToolUse hook for Bash that stops risky Git commands before Claude Code executes them. When a command is blocked, Claude receives a message that it lacks authority to use it.
Which Git commands does the hook block?
The hook blocks git push, git reset --hard, git clean -f variants, git branch -D, git checkout ., and git restore .. You can change the blocked patterns in the copied script.