From mattpocock-skills
Sets up PreToolUse hooks in Claude Code to block dangerous git commands (push, reset --hard, clean, branch -D). Useful when you want to prevent accidental destructive git operations.
How this skill is triggered — by the user, by Claude, or both
Slash command
/mattpocock-skills:git-guardrails-claude-codeThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
设置一个 PreToolUse hook,在 Claude 执行危险 git commands 前拦截并阻止它们。
设置一个 PreToolUse hook,在 Claude 执行危险 git commands 前拦截并阻止它们。
git push(包括 --force 在内的所有 variants)git reset --hardgit clean -f / git clean -fdgit branch -Dgit checkout . / git restore .被阻止时,Claude 会看到一条 message,说明它无权访问这些 commands。
询问用户:只为当前 project 安装(.claude/settings.json),还是为所有 projects 安装(~/.claude/settings.json)?
bundled script 位于:scripts/block-dangerous-git.sh
根据 scope 复制到目标位置:
.claude/hooks/block-dangerous-git.sh~/.claude/hooks/block-dangerous-git.sh用 chmod +x 让它可执行。
添加到对应 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。
询问用户是否要在 blocked list 中添加或移除 patterns。相应编辑复制后的 script。
运行快速测试:
echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>
应以 code 2 退出,并向 stderr 打印 BLOCKED message。
npx claudepluginhub vinvcn/mattpocock-skills-zh-cnSets up Claude Code hooks to block dangerous git commands (push, reset --hard, clean, branch -D) before execution. Useful for preventing destructive git operations in Claude Code.
Installs PreToolUse hooks that block destructive git/shell commands (force-push to protected branches, git reset --hard with unstaged work, rm -rf outside worktree). Useful before unattended runs or after a near-miss.
Installs PreToolUse hooks that block dangerous git commands (push, reset --hard, clean, branch -D, checkout ., restore .) before execution.