git-guardrails-claude-code
What it does
It adds a safety check to Claude Code that stops certain git commands from running. It is for people who want to prevent accidental damage to their code repository.
When to use it
- ✓When you want to stop the agent from pushing code by accident.
- ✓When you want to block destructive commands such as git reset --hard.
- ✓When you want these safety rules applied to one project or to all of your projects.
Skill link
https://github.com/mattpocock/skills/blob/main/skills/misc/git-guardrails-claude-code/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agent’s skills folder.
Example of what to ask your AI
“Set up safety hooks so Claude Code cannot push to or reset my git repo by accident.”
5 new skills a week. 1 minute to read.
How it works
The agent copies a hook script into your project or home settings and links it in the settings file. When a blocked command is tried, the hook stops it and tells the agent it lacks authority to run it.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
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.
FAQ
Which git commands are blocked?
Pushes in all forms, git reset --hard, git clean -f, git branch -D, and checkout or restore applied to all files.
Can I choose where the rules apply?
Yes. The skill asks whether to install them for this project only or for all projects.
What happens when a command is blocked?
Claude sees a message saying it does not have authority to access that command.
Details
Original description by the author
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.
repo: mattpocock/skills
Popularity
- GitHub stars
- 310k
- Ranking position
- #120
- License
- MIT
- Updated
- 11/1/2026
Comments
- …
More skills from this repository
ask-matt
310kLets your agent act as a guide to a set of coding skills, pointing you to the right one for your situation, from idea to shipped code.
code-review
310kChecks your changes against your repo's coding standards and the original spec, running both reviews side by side before you merge.
domain-modeling
310kBuilds a shared project glossary and records key design decisions, so your code uses the same words your team uses.
implement-spec
310kTurns a finished spec and its tickets into working code, handling the tickets in order and bringing them together on one branch.
Similar skills
openclaw-update
394kHelps your agent update an existing OpenClaw Gateway, choosing the update method that fits how it was installed and keeping your session working.
diagnose-gateway
394kHelps your agent find out why your OpenClaw Gateway is failing by checking settings, channels, and ports without changing anything.
crabbox-apps
394kHelps your agent open and run an app on a temporary cloud machine, then show its view in your chat side panel.