setup-hooks

Installs and removes PreToolUse hooks that guard risky shell commands in Claude Code and Codex.

1|Updated Mar 18, 2026
One-click install
npx skills add https://github.com/psw7205/skills --skill setup-hooks-psw7205
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: setup-hooks
Source: https://github.com/psw7205/skills/tree/main/skills/setup-hooks
Command: npx skills add https://github.com/psw7205/skills --skill setup-hooks-psw7205

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) components.

What problem does it solve? AI coding agents can run destructive shell commands like git clean -fd, git reset --hard, or git push --force that wipe uncommitted work or remote history with no recovery path. This Skill installs safety hooks into Claude Code and Codex that intercept those commands before execution: recoverable destructive commands get an automatic stash backup first, while unrecoverable ones like force push are denied outright. ## Core Features & Use Cases - Auto-backup before destructive git commands: Rewrites git clean, git reset --hard, git restore, and git checkout path operations to run a stash backup first, preserving the working tree as a recovery point. - Deny unrecoverable operations: Blocks git push --force and rg --replace misuse, including the rg -rn flag-cluster trap where -r silently consumes the next letter as a replacement value. - Dual-agent support: Registers hooks in ~/.claude/settings.json for Claude Code (rewrite + deny policy) and ~/.codex/hooks.json for Codex (deny-only policy), with install and removal procedures. - Use Case: After an agent accidentally ran git clean -fd and deleted hours of uncommitted work, ask the agent to install these hooks so every future destructive command is either backed up to a stash or blocked with a corrective message. ## Quick Start Ask the agent to install the shell command safety hooks into your Claude Code settings so destructive git commands are backed up or blocked.

Frequently Asked Questions about setup-hooks

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I block destructive git commands from AI coding agents?▼

Install a PreToolUse hook that classifies each shell command before execution. This Skill registers guard hooks in Claude Code settings.json or Codex hooks.json that rewrite recoverable commands like git clean with an automatic stash backup and deny unrecoverable ones like git push --force.

How to prevent git clean or git reset --hard from losing uncommitted work?▼

The hook prepends an auto-backup script that runs git stash push --include-untracked, restores the tree, and only then lets the original command run. If the restore fails, the script exits non-zero so the destructive command never executes, and the work stays recoverable in the stash.

Does this hook work with both Claude Code and Codex CLI?▼

Yes, but with different policies. Claude Code supports command rewriting, so recoverable commands get an auto-backup injected. Codex does not honor rewrites, so its hook denies the same commands and tells the agent to run the backup script manually before retrying.

Why does rg -rn produce wrong search results instead of an error?▼

In ripgrep, -r is --replace and consumes a value, so rg -rn foo makes every match print the literal n while exiting 0. The companion hook repairs -rn-style clusters by stripping the -r, while space-separated rg -r value forms are denied since intent cannot be inferred.

Will the guard block commands mentioned inside quotes or commit messages?▼

No. Classification uses a quote-aware shell lexer that splits commands only on operators outside quotes, so echo 'git clean -fd' or a commit message mentioning git reset --hard passes through. Heredoc bodies are treated as data, not commands.

What are the limitations of shell command guard hooks?▼

The hook is a mistake-prevention device, not a sandbox. Commands hidden inside bash -c strings, command substitution, xargs, or heredoc bodies bypass classification. It also requires python3 at runtime and silently passes everything if its sibling modules fail to import.