git-guardrails-claude-code
设置 Claude Code hooks,在危险 git commands(push、reset --hard、clean、branch -D 等)执行前阻止它们。适用于用户想防止破坏性 git 操作、添加 git safety hooks,或在 Claude Code 中阻止 git push/reset 时。
- License 许可
- MIT
- 最近更新
- 2026-11-10
- Stars
- ★ 5.7k
- Forks
- 441
01 前言 Front Matter
- name
- git-guardrails-claude-code
- description
- 设置 Claude Code hooks,在危险 git commands(push、reset --hard、clean、branch -D 等)执行前阻止它们。适用于用户想防止破坏性 git 操作、添加 git safety hooks,或在 Claude Code 中阻止 git push/reset 时。
02 资源文件 2
- 参考文档agents/openai.yaml112 B
- 脚本scripts/block-dangerous-git.sh507 B
03 SKILL.md 正文
设置 Git Guardrails
设置一个 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。
步骤
1. 询问安装范围
询问用户:只为当前 project 安装(.claude/settings.json),还是为所有 projects 安装(~/.claude/settings.json)?
2. 复制 hook 脚本
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. 把 hook 加入 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. 询问是否定制
询问用户是否要在 blocked list 中添加或移除 patterns。相应编辑复制后的 script。
5. 验证
运行快速测试:
echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>
应以 code 2 退出,并向 stderr 打印 BLOCKED message。
04 同仓库更多 Skills
writing-for-agents
为 agent 编写文档。适用于创建或编辑 skills,或修改 AGENTS.md 或 CLAUDE.md 时。
wait-what
停一下:刚才那条消息没有说清楚,重新表述一遍。
to-questionnaire
把你无法完整回答的 decision 转成一份交给他人填写的 questionnaire。
teach
在这个工作区中教用户一个新技能或概念。
handoff
把当前对话压缩成交接文档,让另一个代理接手。
grilling
围绕计划、decision 或 idea 持续追问用户。适用于用户想对自己的思路做压力测试,或使用任何 “grill” 触发措辞时。