Claude Cursor Skill

stop-destructive-git

Use when asked to block git push, stop force-pushing, add a git safety hook or guardrails, protect a branch, or stop an agent resetting work. Installs a Claude Code PreToolUse hook that parses Bash commands into real tokens and blocks destructive git actions, verified with piped

LLM Mart · 0 points · 0 views 0 listing impressions 0 install-command copies
Download heyvhuang-copybook-skills-plugins_skills_skills_stop-destructive-git-c2e4cc2.zip · 3 KB

Install

skills CLI npx skills add https://github.com/Heyvhuang/copybook-skills/tree/main/plugins/skills/skills/stop-destructive-git
Claude Code claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install heyvhuang-copybook-skills@llmmart
Git git clone https://github.com/Heyvhuang/copybook-skills.git

The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole heyvhuang/copybook-skills collection as a plugin from our marketplace. Git is the plain clone.

Skill manifest

Block destructive git commands

Install a Claude Code PreToolUse hook that parses Bash commands as shell tokens and exits 2 before destructive git actions run.

1. Ask the hook scope

Ask whether to install for this project or every project before writing files:

  • This project: .claude/hooks/stop-destructive-git.py and .claude/settings.json
  • Every project: ~/.claude/hooks/stop-destructive-git.py and ~/.claude/settings.json

2. Install the token-parsing hook script

Write an executable Python 3 script (#!/usr/bin/env python3, then chmod +x) that reads JSON from stdin and extracts tool_input.command. Exit 0 when the field is missing or empty.

Parse the command string instead of matching substrings:

  • Tokenize with a real shell lexer (such as Python shlex in POSIX punctuation mode, keeping newlines as separators) so quoted strings like commit messages stay single tokens.
  • Split tokens into command segments on shell operators (&&, ||, |, &, command separators, and newlines) outside quotes.
  • In each segment, skip leading KEY=VAL assignments, match git, and skip global git options before the subcommand (-C <dir>, -c <k=v>, --git-dir, --work-tree, --namespace, -p, --no-pager, --bare), consuming separate or = option values.
  • Keep one editable blocked-rule list near the top of the script instead of asking customization questions during setup. Block push (all forms), reset --hard, clean with -f or --force (including combined flags like -fdx), branch -D (or --delete with force), and forced broad discards (checkout -f, checkout ., checkout -- ., restore ., restore --worktree .).
  • On a match, print BLOCKED: <reason> to stderr and exit 2. Otherwise exit 0.

3. Merge the hook into Claude Code settings

Create or update the settings file for the chosen scope. Merge a Bash matcher entry into hooks.PreToolUse without removing other settings or existing hooks:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/stop-destructive-git.py"
          }
        ]
      }
    ]
  }
}

For every-project scope, point command to ~/.claude/hooks/stop-destructive-git.py.

4. Verify with piped JSON tests

Pipe simulated hook JSON into the installed script without executing any git command (swap the path if installed for every project):

printf '%s\n' '{"tool_input":{"command":"git -C . push"}}' | .claude/hooks/stop-destructive-git.py

Check every case below and confirm the settings JSON parses cleanly with prior hooks intact:

Piped tool_input.command Expected exit
git -C . push, git push -f origin main, git status && git push 2 (blocked)
git reset --hard HEAD~1, git clean -fdx, git checkout -- ., git branch -D tmp 2 (blocked)
git commit -m "why git push failed", git status, git log 0 (allowed)

5. Report exact scope and limits

Report the exact script path, the exact settings file updated, whether the hook applies to this project or every project, and the piped test results. Note where the editable rule list lives in the script, and state that this hook only guards commands run through Claude Code's Bash tool, not other tools or manual terminal commands.

Based on mattpocock/skills (MIT), rewritten by Copybook.

Files (copybook-skills)
  • CHANGES.md 366 B
    Loading…
  • example.md 1 KB
    Loading…
  • LICENSE 1 KB · in bundle
  • SKILL.md 3.8 KB
    ---
    name: stop-destructive-git
    description: "Use when asked to block git push, stop force-pushing, add a git safety hook or guardrails, protect a branch, or stop an agent resetting work. Installs a Claude Code PreToolUse hook that parses Bash commands into real tokens and blocks destructive git actions, verified with piped JSON tests. Not for keeping parallel work apart in separate branches (start-git-worktree)."
    license: MIT (see LICENSE)
    ---
    
    # Block destructive git commands
    
    Install a Claude Code `PreToolUse` hook that parses `Bash` commands as shell tokens and exits `2` before destructive git actions run.
    
    ## 1. Ask the hook scope
    
    Ask whether to install for this project or every project before writing files:
    
    - This project: `.claude/hooks/stop-destructive-git.py` and `.claude/settings.json`
    - Every project: `~/.claude/hooks/stop-destructive-git.py` and `~/.claude/settings.json`
    
    ## 2. Install the token-parsing hook script
    
    Write an executable Python 3 script (`#!/usr/bin/env python3`, then `chmod +x`) that reads JSON from stdin and extracts `tool_input.command`. Exit `0` when the field is missing or empty.
    
    Parse the command string instead of matching substrings:
    
    - Tokenize with a real shell lexer (such as Python `shlex` in POSIX punctuation mode, keeping newlines as separators) so quoted strings like commit messages stay single tokens.
    - Split tokens into command segments on shell operators (`&&`, `||`, `|`, `&`, command separators, and newlines) outside quotes.
    - In each segment, skip leading `KEY=VAL` assignments, match `git`, and skip global git options before the subcommand (`-C <dir>`, `-c <k=v>`, `--git-dir`, `--work-tree`, `--namespace`, `-p`, `--no-pager`, `--bare`), consuming separate or `=` option values.
    - Keep one editable blocked-rule list near the top of the script instead of asking customization questions during setup. Block `push` (all forms), `reset --hard`, `clean` with `-f` or `--force` (including combined flags like `-fdx`), `branch -D` (or `--delete` with force), and forced broad discards (`checkout -f`, `checkout .`, `checkout -- .`, `restore .`, `restore --worktree .`).
    - On a match, print `BLOCKED: <reason>` to stderr and exit `2`. Otherwise exit `0`.
    
    ## 3. Merge the hook into Claude Code settings
    
    Create or update the settings file for the chosen scope. Merge a `Bash` matcher entry into `hooks.PreToolUse` without removing other settings or existing hooks:
    
    ```json
    {
      "hooks": {
        "PreToolUse": [
          {
            "matcher": "Bash",
            "hooks": [
              {
                "type": "command",
                "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/stop-destructive-git.py"
              }
            ]
          }
        ]
      }
    }
    ```
    
    For every-project scope, point `command` to `~/.claude/hooks/stop-destructive-git.py`.
    
    ## 4. Verify with piped JSON tests
    
    Pipe simulated hook JSON into the installed script without executing any git command (swap the path if installed for every project):
    
    ```bash
    printf '%s\n' '{"tool_input":{"command":"git -C . push"}}' | .claude/hooks/stop-destructive-git.py
    ```
    
    Check every case below and confirm the settings JSON parses cleanly with prior hooks intact:
    
    | Piped `tool_input.command` | Expected exit |
    |---|---|
    | `git -C . push`, `git push -f origin main`, `git status && git push` | `2` (blocked) |
    | `git reset --hard HEAD~1`, `git clean -fdx`, `git checkout -- .`, `git branch -D tmp` | `2` (blocked) |
    | `git commit -m "why git push failed"`, `git status`, `git log` | `0` (allowed) |
    
    ## 5. Report exact scope and limits
    
    Report the exact script path, the exact settings file updated, whether the hook applies to this project or every project, and the piped test results. Note where the editable rule list lives in the script, and state that this hook only guards commands run through Claude Code's `Bash` tool, not other tools or manual terminal commands.
    
    Based on mattpocock/skills (MIT), rewritten by Copybook.
    

Comments

Sign in to join the conversation.

No comments yet. Start the conversation.

Reviews (0)

No reviews yet.