Git Guardrails Claude Code
Matt PocockCommunityCreated by Matt Pocock
- Skill
- Workflow
- Software development
- MIT
- Low risk
- Has scripts
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a structured procedure to configure PreToolUse hooks within Claude Code. By intercepting bash commands, it prevents the agent from executing potentially harmful Git operations such as forced pushes, hard resets, or branch deletions. The process involves deploying a shell script and updating the agent's configuration file to ensure these commands are blocked before they reach the terminal.
What it does
- Intercepts bash commands executed by Claude Code using PreToolUse hooks.
- Blocks dangerous Git operations including push, reset, clean, and branch deletion.
- Supports both project-specific and global configuration scopes.
- Provides a mechanism to customize the list of blocked commands.
- Includes verification steps to ensure the guardrails are active.
When to use it
- When you want to prevent Claude Code from accidentally modifying or deleting repository history.
- When working in sensitive environments where destructive Git commands should be restricted.
- When you need to enforce safety policies for automated Git operations.
Advantages
- Reduces the risk of accidental data loss or repository corruption.
- Allows for granular control over which Git commands an agent can perform.
- Flexible deployment options for individual projects or global settings.
Usage examples
- “Set up git guardrails for this project.”
- The agent will guide you through copying the block script to your project's .claude/hooks directory and updating the local settings.json to intercept dangerous commands.
- “I want to prevent Claude from ever running git push.”
- The agent will help you install the hook script and verify that any attempt to execute git push is blocked with an error message.
Install
Download the skill as a ZIP:
https://autogineer.com/agent-skills/mattpocock/git-guardrails-claude-code/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip git-guardrails-claude-code.zip -d .agents/skills
Documentation by Matt Pocock
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.shGlobal:
~/.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.
Files
- ATTRIBUTION.mdAdded by AutoGineer886 B
- LICENSELicence file added by AutoGineer1.0 KiB
- SKILL.md2.3 KiB
- agents/openai.yaml112 B
- scripts/block-dangerous-git.shScript507 B
agents/openai.yaml
interface: display_name: "Git Guardrails for Claude Code" short_description: "Block dangerous git commands"
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Security
What our scanner noticed· Low risk
- Scripts that your agent may run
This skill includes scripts that your agent may run. Read them before you use it.
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- Matt Pocock
- Publisher
- Matt Pocock (Community)
- Source
- mattpocock/skills · skills/misc/git-guardrails-claude-code · commit 4ae2317
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by Matt Pocock or Matt Pocock.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download git-guardrails-claude-code.zip