git-guardrails-claude-code
vinvcn/mattpocock-skills-zh-cn
Block dangerous git commands (push, reset, clean) before Claude Code executes them.
What is git-guardrails-claude-code?
Sets up a PreToolUse hook that intercepts and prevents destructive git operations in Claude Code. Use this when you want to add safety guardrails to prevent accidental data loss from commands like git push, git reset --hard, or git branch -D.
- Blocks git push (all variants including --force)
- Blocks git reset --hard
- Blocks git clean -f and git clean -fd
- Blocks git branch -D
- Blocks git checkout . and git restore .
- Configurable per-project or globally across all projects
How to install git-guardrails-claude-code
npx skills add https://github.com/vinvcn/mattpocock-skills-zh-cn --skill git-guardrails-claude-code- Claude Code installed and configured
- Access to .claude/settings.json (project) or ~/.claude/settings.json (global)
- Ability to execute chmod +x on hook scripts
How to use git-guardrails-claude-code
- 1.Decide whether to install for current project only (.claude/settings.json) or globally (~/.claude/settings.json)
- 2.Copy the block-dangerous-git.sh script to the appropriate hooks directory (.claude/hooks/ or ~/.claude/hooks/)
- 3.Make the script executable with chmod +x
- 4.Add the PreToolUse hook configuration to your settings.json file
- 5.Optionally customize the blocked command patterns in the script
- 6.Verify the hook works by testing with: echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>
Use cases
- Prevent Claude from accidentally pushing to production branches
- Stop destructive reset operations during code exploration
- Block branch deletion commands that could lose work
- Add safety checks for teams using Claude Code for development
- Protect main/master branches from unintended force pushes
- Developers using Claude Code
- Teams wanting to prevent accidental git disasters
- Projects with strict git safety requirements
- Anyone concerned about destructive git operations
git-guardrails-claude-code FAQ
Yes. After copying the script, you can edit it to add or remove command patterns from the blocked list.
You choose during setup. Install to .claude/settings.json for current project only, or ~/.claude/settings.json to apply globally to all projects.
Claude receives a message indicating it does not have access to that command, and the operation is prevented.
Yes, this only blocks Claude Code from executing them. You can still run dangerous git commands directly in your terminal.
Run the provided test command: echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>. It should exit with code 2 and print a BLOCKED message to stderr.
Full instructions (SKILL.md)
Source of truth, from vinvcn/mattpocock-skills-zh-cn.
name: git-guardrails-claude-code description: 设置 Claude Code hooks,在危险 git commands(push, reset --hard, clean, branch -D 等)执行前阻止它们。Use when user wants to prevent destructive git operations, add git safety hooks, or block git push/reset in Claude Code.
Setup Git Guardrails
设置一个 PreToolUse hook,在 Claude 执行危险 git commands 前拦截并阻止它们。
What Gets Blocked
git push(包括--force在内的所有 variants)git reset --hardgit clean -f/git clean -fdgit branch -Dgit checkout ./git restore .
被阻止时,Claude 会看到一条 message,说明它无权访问这些 commands。
Steps
1. Ask scope
询问用户:只为当前 project 安装(.claude/settings.json),还是为所有 projects 安装(~/.claude/settings.json)?
2. Copy the hook script
bundled script 位于:scripts/block-dangerous-git.sh
根据 scope 复制到目标位置:
- Project:
.claude/hooks/block-dangerous-git.sh - Global:
~/.claude/hooks/block-dangerous-git.sh
用 chmod +x 让它可执行。
3. Add hook to settings
添加到对应 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"
}
]
}
]
}
}
如果 settings file 已存在,把 hook merge 到现有 hooks.PreToolUse array 中,不要覆盖其他 settings。
4. Ask about customization
询问用户是否要在 blocked list 中添加或移除 patterns。相应编辑复制后的 script。
5. Verify
运行快速测试:
echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>
应以 code 2 退出,并向 stderr 打印 BLOCKED message。
Related skills
More from vinvcn/mattpocock-skills-zh-cn and the wider catalog.

grill-me
Stress-test plans and designs through systematic questioning until shared understanding is reached.

grill-with-docs
Stress-test plans against your domain model and sharpen terminology while updating docs inline.

improve-codebase-architecture
Find architecture friction and propose deepening opportunities to improve testability and AI-navigability.

migrate-to-shoehorn
Replace unsafe `as` type assertions in tests with type-safe shoehorn alternatives.

obsidian-vault
在 Obsidian vault 中使用 wikilinks 和 index notes 搜索、创建并管理 notes。Use when user wants to find, create, or organize notes in Obsidian.

prototype
Build throwaway prototypes to validate designs and state models before committing to production code.