Skip to main content
NEW · APP STORE Now on iOS · macOS · iPad Android & Windows soon GET IT
Prompts Claude Code Git Guardrails Setup

agent safety skill risk: low

Claude Code Git Guardrails Setup

The prompt provides step-by-step instructions to configure PreToolUse hooks that block dangerous git commands like push, reset --hard, and clean in Claude Code, including scope sel…

  • External action: low

SKILL 2 files · 1 folder

SKILL.md
---
name: git-guardrails-claude-code
description: "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."
---
# 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](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`):

```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"
          }
        ]
      }
    ]
  }
}
```

If the settings file already exists, merge the hook into 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:

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

Should exit with code 2 and print a BLOCKED message to stderr.

REQUIRED CONTEXT

  • location of bundled block-dangerous-git.sh script

ROLES & RULES

  1. If the settings file already exists, merge the hook into existing `hooks.PreToolUse` array — don't overwrite other settings.

EXPECTED OUTPUT

Format
plain_text
Constraints
  • follow the exact 5 steps in order
  • ask user for scope and customization preferences
  • provide correct JSON snippets for project vs global settings
  • include verification command and expected output

EXAMPLES

Includes two JSON hook configuration examples (project and global) plus one bash verification test command.

CAVEATS

Dependencies
  • Requires bundled script at scripts/block-dangerous-git.sh
Missing context
  • Content of block-dangerous-git.sh
  • Exact merge logic or conflict resolution for PreToolUse arrays
Ambiguities
  • References external script path without providing its content
  • Merge instructions for existing settings files are high-level only

QUALITY

OVERALL
0.65
CLARITY
0.85
SPECIFICITY
0.80
REUSABILITY
0.35
COMPLETENESS
0.65

IMPROVEMENT SUGGESTIONS

  • Inline the full hook script content so the prompt is self-contained
  • Add explicit placeholders or variables for paths and settings files to improve reusability

USAGE

Copy the prompt above and paste it into your AI of choice — Claude, ChatGPT, Gemini, or anywhere else you're working. Replace any placeholder sections with your own context, then ask for the output.

MORE FOR AGENT