Git Guardrails Claude Code

by vinvcn3f92a83668efNo license4.6K starsListed Oct 8, 2026Updated Oct 8, 2026Repository updated today

设置 Claude Code hooks,在危险 git commands(push、reset --hard、clean、branch -D 等)执行前阻止它们。适用于用户想防止破坏性 git 操作、添加 git safety hooks,或在 Claude Code 中阻止 git push/reset 时。

AI-generated overview

Sets up Claude Code hooks that block dangerous git commands such as push, reset --hard, clean and branch -D.

What it does
This skill installs a PreToolUse hook in Claude Code that intercepts and blocks dangerous git commands before they run, including git push, git reset --hard, git clean -f/-fd, git branch -D, and git checkout . / git restore . It ships a hook script that is copied into either the project's .claude/hooks directory or the user's global ~/.claude/hooks directory, made executable, and registered in the matching settings file. It also guides the user through choosing project or global scope, merging the hook into existing settings, customizing the blocked pattern list, and verifying the hook with a test command.
When to use it
Use this when a user wants to prevent destructive git operations in Claude Code, add git safety hooks, or block git push or reset commands. It fits both per-project and all-projects setups.
Requirements
Requires Claude Code with hook support and a shell environment. It ships an executable script (scripts/block-dangerous-git.sh) that must be copied and made executable with chmod +x, and it writes to .claude/settings.json or ~/.claude/settings.json. No credentials or network access are needed.

Setup Git Guardrails

设置一个 PreToolUse hook,在 Claude 执行危险 git commands 前拦截并阻止它们。

What Gets Blocked

  • git push(包括 --force 在内的所有 variants)
  • git reset --hard
  • git clean -f / git clean -fd
  • git branch -D
  • git 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 [blocked]

根据 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):

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

Global (~/.claude/settings.json):

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

运行快速测试:

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

应以 code 2 退出,并向 stderr 打印 BLOCKED message。

Source and attribution

Source:vinvcn/mattpocock-skills-zh-cninskills/misc/git-guardrails-claude-codeat commit3f92a83

License: No license

Content belongs to its original authors. SourceWeft indexes it from a public repository.

Report or request removal