$npx -y skills add stevesolun/ctx --skill git-guardrails-claude-codeSet 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.
| 1 | # Setup Git Guardrails |
| 2 | |
| 3 | Sets up a PreToolUse hook that intercepts and blocks dangerous git commands before Claude executes them. |
| 4 | |
| 5 | ## What Gets Blocked |
| 6 | |
| 7 | - `git push` (all variants including `--force`) |
| 8 | - `git reset --hard` |
| 9 | - `git clean -f` / `git clean -fd` |
| 10 | - `git branch -D` |
| 11 | - `git checkout .` / `git restore .` |
| 12 | |
| 13 | When blocked, Claude sees a message telling it that it does not have authority to access these commands. |
| 14 | |
| 15 | ## Steps |
| 16 | |
| 17 | ### 1. Ask scope |
| 18 | |
| 19 | Ask the user: install for **this project only** (`.claude/settings.json`) or **all projects** (`~/.claude/settings.json`)? |
| 20 | |
| 21 | ### 2. Copy the hook script |
| 22 | |
| 23 | The bundled script is at: [scripts/block-dangerous-git.sh](scripts/block-dangerous-git.sh) |
| 24 | |
| 25 | Copy it to the target location based on scope: |
| 26 | |
| 27 | - **Project**: `.claude/hooks/block-dangerous-git.sh` |
| 28 | - **Global**: `~/.claude/hooks/block-dangerous-git.sh` |
| 29 | |
| 30 | Make it executable with `chmod +x`. |
| 31 | |
| 32 | ### 3. Add hook to settings |
| 33 | |
| 34 | Add to the appropriate settings file: |
| 35 | |
| 36 | **Project** (`.claude/settings.json`): |
| 37 | |
| 38 | ```json |
| 39 | { |
| 40 | "hooks": { |
| 41 | "PreToolUse": [ |
| 42 | { |
| 43 | "matcher": "Bash", |
| 44 | "hooks": [ |
| 45 | { |
| 46 | "type": "command", |
| 47 | "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-dangerous-git.sh" |
| 48 | } |
| 49 | ] |
| 50 | } |
| 51 | ] |
| 52 | } |
| 53 | } |
| 54 | ``` |
| 55 | |
| 56 | **Global** (`~/.claude/settings.json`): |
| 57 | |
| 58 | ```json |
| 59 | { |
| 60 | "hooks": { |
| 61 | "PreToolUse": [ |
| 62 | { |
| 63 | "matcher": "Bash", |
| 64 | "hooks": [ |
| 65 | { |
| 66 | "type": "command", |
| 67 | "command": "~/.claude/hooks/block-dangerous-git.sh" |
| 68 | } |
| 69 | ] |
| 70 | } |
| 71 | ] |
| 72 | } |
| 73 | } |
| 74 | ``` |
| 75 | |
| 76 | If the settings file already exists, merge the hook into existing `hooks.PreToolUse` array — don't overwrite other settings. |
| 77 | |
| 78 | ### 4. Ask about customization |
| 79 | |
| 80 | Ask if user wants to add or remove any patterns from the blocked list. Edit the copied script accordingly. |
| 81 | |
| 82 | ### 5. Verify |
| 83 | |
| 84 | Run a quick test: |
| 85 | |
| 86 | ```bash |
| 87 | echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script> |
| 88 | ``` |
| 89 | |
| 90 | Should exit with code 2 and print a BLOCKED message to stderr. |