agent-git-guard
v1.0.0
Published
Never let your AI coding agents destroy your git repository. Zero-dependency safety guardrails for Claude Code, Cursor, Windsurf, Aider, and local CLI agents.
Maintainers
Readme
🛡️ agent-git-guard
Never let your AI coding agents destroy your git repository again.
Zero-dependency, ultra-lightweight (<50KB, ~3ms) safety guardrails for Claude Code, Cursor, Windsurf, Aider, and local CLI agents.
💥 The Problem
Autonomous AI coding agents (Claude Code, Cursor Agent, Windsurf, Aider, OpenDevin) are extraordinarily capable, but they lack instinctual risk boundaries. A single hallucinated command can cause irreversible disasters:
git push -f origin mainoverwriting months of team commits.git reset --hardsilently discarding uncommitted user files.git clean -fdpermanently deleting untracked assets and newly created files.git checkout .orgit restore .wiping unstaged work.git add .envaccidentally committing production database credentials and API keys.
agent-git-guard intercepts and neutralizes these destructive actions before they execute.
⚡ Quick Start (1-Second Install)
In your project root, run:
npx agent-git-guard initThat's it! agent-git-guard automatically configures:
- Claude Code (
.claude/settings.jsonPreToolUse hook) - Git Native Hooks (
.git/hooks/pre-push&pre-commit) - Cursor (
.cursor/rules/agent-git-guard.mdc)
🎯 What Gets Blocked?
| Risk Level | Target Action | Why it's Blocked |
|---|---|---|
| CRITICAL | git push -f / --force | Permanently overwrites remote history |
| CRITICAL | git reset --hard | Destroys uncommitted and committed work |
| CRITICAL | git clean -f / -fd | Irreversibly deletes untracked files |
| CRITICAL | rm -rf .git / rm -rf * | Wipes repository and workspace |
| HIGH | git push (direct) | Requires explicit human review before remote sync |
| HIGH | git branch -D | Force deletes branches without merge verification |
| HIGH | git checkout . / restore . | Discards all uncommitted file modifications |
| HIGH | git add .env / id_rsa / *.pem | Prevents staging secrets and private keys |
🧠 Intelligent Parsing (Zero False Positives)
agent-git-guard parses compound shell commands (&&, ||, ;, subshells like bash -c '...'), while strictly preserving benign operations:
- ✅
git commit -m "fix: resolve git push remote issue"(Allowed: quoted string) - ✅
echo "git reset --hard should be avoided"(Allowed: echo / print) - 🛑
npm test && git push origin main(Blocked: chained destructive push) - 🛑
bash -c "git reset --hard"(Blocked: nested subshell)
🛠️ CLI Usage
1. Manual Command Check
Test if any command violates guard rules:
npx agent-git-guard check "git push origin main"
# Output:
# 🛑 Command VIOLATES safety guardrails:
# 🚨 [HIGH] git-push
# Reason: Direct git push by AI agent without explicit human permission
# Advice: Do not push directly to remote. Ask the human user to review and push.2. Inspect Protection Status
Check which agent layers are currently protected in your workspace:
npx agent-git-guard status3. Granular Installation Options
# Global install for Claude Code (protects all projects)
npx agent-git-guard init --global
# Install Git native hooks only
npx agent-git-guard init --git-hooks
# Install Claude hook only
npx agent-git-guard init --claude
# Install Cursor rules only
npx agent-git-guard init --cursor🔓 Bypassing When Intended (Human Override)
When a human legitimately needs to push from an agent session, simply set:
ALLOW_AGENT_PUSH=1 git push origin main📦 Programmatic SDK
You can also use agent-git-guard directly inside your own Node.js or TypeScript agent orchestrator:
import { checkCommand } from 'agent-git-guard';
const result = checkCommand('npm test && git push');
if (!result.allowed) {
console.log(`Action blocked: ${result.violations[0].rule.description}`);
}📜 License
MIT © OIYO Lab
