AGENT SKILL

git-guardrails-claude-code

配置 Claude Code hook,在危险的 Git 命令(push、reset --hard、clean、branch -D 等)执行前拦截。在用户想防止破坏性 Git 操作、添加 Git 安全 hook,或在 Claude Code 中拦截 git push/reset 时使用。

License 许可
MIT
最近更新
2026-08-19
Stars
★ 0
Forks
0

01 前言 Front Matter

name
git-guardrails-claude-code
description
配置 Claude Code hook,在危险的 Git 命令(push、reset --hard、clean、branch -D 等)执行前拦截。在用户想防止破坏性 Git 操作、添加 Git 安全 hook,或在 Claude Code 中拦截 git push/reset 时使用。

02 资源文件 2

03 SKILL.md 正文

配置 Git 护栏

设置一个 PreToolUse hook,在 Claude 执行危险的 Git 命令之前拦截并阻止它们。

会拦截什么

  • git push(所有变体,包括 --force)
  • git reset --hard
  • git clean -f / git clean -fd
  • git branch -D
  • git checkout . / git restore .

被拦截时,Claude 会看到一条消息,告知它没有权限使用这些命令。

步骤

1. 询问作用范围

询问用户:仅安装到当前项目(.claude/settings.json)还是所有项目(~/.claude/settings.json)?

2. 复制 hook 脚本

附带脚本位于:scripts/block-dangerous-git.sh

按作用范围复制到目标位置:

  • 项目:.claude/hooks/block-dangerous-git.sh
  • 全局:~/.claude/hooks/block-dangerous-git.sh

用 chmod +x 使其可执行。

3. 将 hook 加入设置

添加到对应的设置文件:

项目(.claude/settings.json):

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-dangerous-git.sh"
          }
        ]
      }
    ]
  }
}

全局(~/.claude/settings.json):

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "~/.claude/hooks/block-dangerous-git.sh"
          }
        ]
      }
    ]
  }
}

如果设置文件已存在,把 hook 合并进现有的 hooks.PreToolUse 数组——不要覆盖其他设置。

4. 询问自定义

询问用户是否要从拦截列表中增删任何模式。据此编辑已复制的脚本。

5. 验证

快速测试:

echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>

应以退出码 2 结束,并向 stderr 打印 BLOCKED 消息。

04 同仓库更多 Skills