git-guardrails-claude-code
Install a Claude Code hook that stops destructive git commands (push, reset --hard, clean, branch -D, and friends) before they run. Use when the user wants to guard against dangerous git operations, add git safety hooks, or block git push/reset inside Claude Code.
- License 许可
- Apache-2.0
- 最近更新
- 2026-11-06
- Stars
- ★ 7
- Forks
- 0
01 前言 Front Matter
- name
- git-guardrails-claude-code
- description
- Install a Claude Code hook that stops destructive git commands (push, reset --hard, clean, branch -D, and friends) before they run. Use when the user wants to guard against dangerous git operations, add git safety hooks, or block git push/reset inside Claude Code.
02 资源文件 1
03 SKILL.md 正文
Git guardrails for Claude Code
Wire up a PreToolUse hook that inspects every Bash command Claude is about to run and refuses the destructive git operations before they touch the repository.
What the guard refuses
git pushin any form, including--forcegit reset --hardgit clean -fandgit clean -fdgit branch -Dgit checkout .andgit restore .
When a command matches, the hook exits with code 2 and Claude receives a message stating it is not permitted to run that command, so it stops rather than retrying.
Steps
1. Confirm the scope
Ask whether the guard should cover this project only (.claude/settings.json) or every project (~/.claude/settings.json). The answer decides where the script and the hook entry go.
2. Place the hook script
The bundled script lives at scripts/block-dangerous-git.sh. Copy it to match the chosen scope:
- Project:
.claude/hooks/block-dangerous-git.sh - Global:
~/.claude/hooks/block-dangerous-git.sh
Then mark it executable with chmod +x.
3. Register the hook in settings
Add the hook to the settings file for the chosen scope.
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"
}
]
}
]
}
}
When the settings file already exists, merge this entry into the existing hooks.PreToolUse array and leave the rest of the file untouched.
4. Offer to tune the pattern list
Ask whether any patterns should be added or dropped, then edit the DANGEROUS_PATTERNS array in the copied script to match.
5. Confirm it works
Feed a sample command through the script and check the result:
echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>
A working guard exits with code 2 and prints a BLOCKED line to stderr.
04 同仓库更多 Skills
writing-great-skills
Reference for writing and editing skills well — the vocabulary and principles that make a skill predictable.
technical-writing
House style for technical writing — the voice, sentence craft, formatting, and user-facing-copy rules that make docs and product text clear, direct, and consistent. Use when writing or editing a README, docs, a spec, release notes, a changelog, help text, or any error/CLI/UI copy a person reads; when reviewing a diff that touches prose or messages; or when a project needs a writing standard. Reference-heavy mechanics live in REFERENCE.md.
teach
Teach the user a new skill or concept, within this workspace.
handoff
Compact the current conversation into a handoff document for another agent to pick up.
grilling
Interrogate the user's plan or design one decision at a time until you both share the same understanding. Use when the user wants to pressure-test an idea before building it, or reaches for any 'grill' trigger phrase.
grill-me
A relentless, one-decision-at-a-time interview that sharpens a plan or design.