跳到正文
Skillpedia
部署与云(DevOps)310k · #120

git-guardrails-claude-code

它能做什么

它为 Claude Code 增加一项安全检查,阻止某些 git 命令运行。它适合希望防止代码仓库被意外破坏的人。

适用场景

  • ✓你想阻止智能体意外推送代码的时候。
  • ✓你想拦截 git reset --hard 等具有破坏性的命令的时候。
  • ✓你希望这些安全规则适用于某一个项目或你的所有项目的时候。

技能链接

https://github.com/mattpocock/skills/blob/main/skills/misc/git-guardrails-claude-code/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/mattpocock/skills/blob/main/skills/misc/git-guardrails-claude-code/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请设置安全钩子,使 Claude Code 不能意外推送到我的 git 仓库,或将其重置。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体把一个钩子脚本复制到你的项目或用户主目录的设置中,并在设置文件中将其关联起来。当有人尝试执行被拦截的命令时,钩子会阻止它,并告诉智能体它没有运行该命令的权限。

技术内容(原始 SKILL.md)

作者在 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 --hard
  • git clean -f / git clean -fd
  • git branch -D
  • git 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.

常见问题

哪些 git 命令会被拦截?

各种形式的推送、git reset --hard、git clean -f、git branch -D,以及应用于所有文件的 checkout 或 restore。

我可以选择规则的适用范围吗?

可以。技能会询问是只为此项目安装,还是为所有项目安装。

命令被拦截时会发生什么?

Claude 会看到一条消息,说明它没有访问该命令的权限。

详情

作者的原始描述

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

热度

GitHub 星标
310k
排名
#120
许可证
MIT
更新时间
2026/11/1

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能