Skip to content
git-safety-guard logo

Git Safety Guard

git-safety-guard

Blocks destructive git and filesystem commands before execution. Prevents accidental loss of uncommitted work from git checkout --, git reset --hard, rm -rf, and similar destructive operations. Works as a Claude Code PreToolUse hook with fail-open semantics.

SKILL.md

Full skill instructions

Git Safety Guard

Use this skill when setting up or configuring protection against destructive git/​filesystem commands in Claude Code.

Overview

Git Safety Guard intercepts Bash commands before execution and blocks dangerous operations that could permanently destroy uncommitted work or important files. The guard uses regex pattern matching with an allowlist to distinguish between safe and dangerous variants of commands.

Key Features:

  • terraphim-agent guard - CLI command for pattern checking
  • PreToolUse hook - Intercept Claude Code tool calls before execution
  • Allowlist support - Safe patterns override dangerous patterns
  • Fail-open semantics - If guard fails, commands pass through

Architecture

Claude Code PreToolUse
      |
      v
git_safety_guard.sh (shell wrapper)
      |
      v
terraphim-agent guard --json "$COMMAND"
      |
      v
Pattern Matching (Regex)
      |
      +---> SAFE_PATTERNS (allowlist) -> Allow
      |
      +---> DESTRUCTIVE_PATTERNS -> Block with reason
      |
      +---> No match -> Allow

Commands Blocked

Command PatternReason
git checkout -- <files>Discards uncommitted changes permanently
git restore <files>Discards uncommitted changes (except --staged)
git reset --hardDestroys all uncommitted changes
git reset --mergeCan lose uncommitted changes
git clean -fRemoves untracked files permanently
git push --forceDestroys remote history
git push -fSame as --force
git branch -DForce-deletes branch without merge check
rm -rf (non-temp paths)Recursive file deletion
git stash dropPermanently deletes stashed changes
git stash clearDeletes ALL stashed changes
git commit --no-verifyBypasses pre-commit and commit-msg hooks
git commit -nSame as --no-verify
git push --no-verifyBypasses pre-push hooks

Commands Explicitly Allowed

Command PatternWhy Safe
git checkout -b <branch>Creates new branch, doesn't modify files
git checkout --orphanCreates orphan branch
git restore --stagedOnly unstages files, doesn't discard changes
git clean -n / --dry-runPreview only, no actual deletion
git push --force-with-leaseSafer force push with remote check
rm -rf /​tmp/​...Temp directories are designed for ephemeral data
rm -rf /​var/​tmp/​...System temp directory
rm -rf $TMPDIR/​...User's temp directory

For Humans

Quick Start

# Install terraphim-agent from GitHub releases (latest version)
# macOS ARM64 (Apple Silicon)
gh release download --repo terraphim/​terraphim-ai \
  --pattern "terraphim-agent-aarch64-apple-darwin" --dir /​tmp
chmod +x /​tmp/​terraphim-agent-aarch64-apple-darwin
mv /​tmp/​terraphim-agent-aarch64-apple-darwin ~/​.cargo/​bin/​terraphim-agent

# macOS x86_64 (Intel)
# gh release download --repo terraphim/​terraphim-ai \
#   --pattern "terraphim-agent-x86_64-apple-darwin" --dir /​tmp

# Linux x86_64
# gh release download --repo terraphim/​terraphim-ai \
#   --pattern "terraphim-agent-x86_64-unknown-linux-gnu" --dir /​tmp

# Note: crates.io version (cargo install terraphim_agent) is outdated (v1.0.0)
# and missing the guard command. Use GitHub releases for latest features.

# Test guard command
echo "git checkout -- file.txt" | terraphim-agent guard --json
# Output: {"decision":"block","reason":"git checkout -- discards...","command":"..."}

echo "git checkout -b new-branch" | terraphim-agent guard --json
# Output: {"decision":"allow","command":"git checkout -b new-branch"}

Installation

Project-local installation:

# Copy hook script
cp .claude/​hooks/​git_safety_guard.sh /​your/​project/​.claude/​hooks/

# Add to .claude/​settings.local.json
{
  "hooks": {
    "PreToolUse": [{
      "matcher": "Bash",
      "hooks": [{
        "type": "command",
        "command": ".claude/​hooks/​git_safety_guard.sh"
      }]
    }]
  }
}

Global installation (~/​.claude/):

# Run install script with --global flag
./​scripts/​install-terraphim-hooks.sh --global

CLI Usage

# Check a command (JSON output)
echo "git reset --hard" | terraphim-agent guard --json

# Check a command (human output - exits with code 1 if blocked)
echo "rm -rf /​home/​user" | terraphim-agent guard

# Fail-open mode (always exit 0)
echo "git checkout -- ." | terraphim-agent guard --fail-open

For AI Agents

Detecting Guard Availability

# Check if terraphim-agent is available
if command -v terraphim-agent >/​dev/​null 2>&1; then
    echo "Guard available"
elif [ -x "./​target/​release/​terraphim-agent" ]; then
    AGENT="./​target/​release/​terraphim-agent"
fi

Using Guard in Hooks

PreToolUse Hook Pattern:

#!/​bin/​bash
# Read JSON input
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name')
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command')

# Only process Bash commands
[ "$TOOL_NAME" != "Bash" ] && exit 0

# Check command
RESULT=$(terraphim-agent guard --json <<< "$COMMAND")

# If blocked, output deny decision
if echo "$RESULT" | jq -e '.decision == "block"' >/​dev/​null; then
    REASON=$(echo "$RESULT" | jq -r '.reason')
    cat <<EOF
{
  "hookSpecificOutput": {
    "hookEventName": "PreToolUse",
    "permissionDecision": "deny",
    "permissionDecisionReason": "BLOCKED: $REASON"
  }
}
EOF
fi

exit 0

Guard Response Format

Blocked command:

{
  "decision": "block",
  "reason": "git checkout -- discards uncommitted changes permanently. Use 'git stash' first.",
  "command": "git checkout -- file.txt",
  "pattern": "git\\s+checkout\\s+--\\s+"
}

Allowed command:

{
  "decision": "allow",
  "command": "git checkout -b new-branch"
}

Error Handling

The guard uses fail-open semantics:

  • If terraphim-agent is not found: pass through unchanged
  • If pattern matching fails: allow command
  • Errors logged to stderr only in verbose mode

Enable verbose mode:

export TERRAPHIM_VERBOSE=1

What Happens When Blocked

When Claude tries to run a blocked command, it receives feedback like:

BLOCKED by git_safety_guard

Reason: git checkout -- discards uncommitted changes permanently. Use 'git stash' first.

Command: git checkout -- file.txt

If this operation is truly needed, ask the user for explicit permission and have them run the command manually.

The command never executes. Claude sees this feedback and should ask the user for help.

Testing

# Manual test - should be blocked
echo "git checkout -- test.txt" | terraphim-agent guard --json

# Manual test - should be allowed
echo "git checkout -b feature" | terraphim-agent guard --json

# Run unit tests
cargo test -p terraphim_agent guard_patterns

# Test hook script
echo '{"tool_name":"Bash","tool_input":{"command":"git reset --hard"}}' | \
  .claude/​hooks/​git_safety_guard.sh

Troubleshooting

IssueSolution
Hook not triggeringCheck .claude/​settings.local.json configuration
Command not blockedVerify pattern matches with terraphim-agent guard --json
Agent not foundBuild with cargo build -p terraphim_agent --release
Permission deniedRun chmod +x .claude/​hooks/​git_safety_guard.sh
jq not foundInstall jq: brew install jq or apt install jq

The Incident That Prompted This

On December 17, 2025, an AI agent ran git checkout -- on multiple files containing hours of uncommitted work from another agent. This destroyed the work instantly and silently. The files were recovered from a dangling Git object via git fsck --lost-found, but it was a close call.

Instructions alone don't prevent accidents. Mechanical enforcement does.

Related Skills

  • terraphim-hooks - Knowledge graph-based text replacement
  • implementation - For building custom guards
  • testing - For validating guard behavior
  • devops - For CI/​CD integration with hooks

More DevOps & CI/CD skills

Project scaffolding, deployment configuration, and CI/CD setup for Google ADK agents.

6K 472.6K
View

Set up tracing, logging, and monitoring for deployed ADK agents across Cloud Trace, BigQuery, and third-party platforms.

6K 472.6K
View

Enterprise Azure infrastructure architect generating Bicep or Terraform from workload descriptions.

1.5K 454.3K
View
azure-kubernetes logo
DevOps & CI/CD

azure-kubernetes

Plan and configure production-ready Azure Kubernetes Service clusters with Day-0 and Day-1 best practices.

1.5K 447.1K
View

Raw mechanical interfaces fusing Swiss typographic print with military terminal aesthetics. Rigid grids, extreme type scale contrast, utilitarian color, analog degradation effects. For data-heavy dashboards, portfolios, or editorial sites that need to feel like declassified blueprints.

92.7K 354.9K
View
just-scrape logo
DevOps & CI/CD

just-scrape

Web search, scraping, extraction, crawling, and monitoring via ScrapeGraph AI CLI.

69 245K
View

Skill for working with Firebase Hosting (Classic). Use this when you want to deploy static web apps, Single Page Apps (SPAs), or simple microservices. Do NOT use for Firebase App Hosting.

462 159.7K
View

Deploy and manage web apps with Firebase App Hosting. Use this skill when deploying Next.js/Angular apps with backends.

462 159.1K
View
deploy-to-vercel logo
DevOps & CI/CD

deploy-to-vercel

Deploy applications and websites to Vercel. Use when the user requests deployment actions like "deploy my app", "deploy and give me the link", "push this live", or "create a preview deployment".

31.9K 146.6K
View
programmatic-seo logo
DevOps & CI/CD

programmatic-seo

Build SEO-optimized pages at scale using templates, data, and proven playbook patterns.

53.3K 140.1K
View

Design and build isolated, reusable Convex backend components with clear boundaries and app-facing wrappers.

63 123.5K
View

Deploy and manage projects on Vercel using token-based authentication. Use when working with Vercel CLI using access tokens rather than interactive login — e.g. "deploy to vercel", "set up vercel", "add environment variables to vercel".

31.9K 116.1K
View

Coding & apps AI tools

Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View
Imagica logo
Coding & apps

Imagica

Build a no-code AI app in minutes.

Freemium
View
E
Coding & apps

Emergent.sh

An IDE for code migration from legacy to modern frameworks through coding agents.

Freemium
View
Wonder Dynamics logo
Coding & apps

Wonder Dynamics

Automate CGI animation in live-action scenes

Paid
View
M
Coding & apps

Mixo

Launch a website in seconds with AI.

Paid
View
AI Code Convert logo
Coding & apps

AI Code Convert

Streamline Your Coding Experience with AI Code Helper

Free
View