Get the FREE Ultimate OpenClaw Setup Guide โ†’
npx machina-cli add skill suryast/free-ai-agent-skills/git-guardian --openclaw
Files (1)
SKILL.md
11.1 KB

๐Ÿ›ก๏ธ Git Guardian

Compatible with Claude Code, Codex CLI, Cursor, Windsurf, and any SKILL.md-compatible agent.

Pre-commit safety checks built for AI-assisted development. AI agents generate code fast โ€” sometimes too fast. Git Guardian catches secrets, sensitive files, merge conflicts, and common AI mistakes before they land in your history.


Triggers

Activate this skill when:

  • "check before commit", "is this safe to commit", "pre-commit check"
  • "any secrets in staged files?", "check for API keys"
  • About to commit AI-generated code
  • "run git guardian", "safety check", "audit staged changes"
  • After a large AI-generated code dump, before pushing
  • User sets up a new repo and wants pre-commit safety

The Full Check Suite

Run these checks against staged changes (or a specified path). Report findings with severity: ๐Ÿ”ด BLOCK, ๐ŸŸก WARN, ๐Ÿ”ต INFO.


Check 1: Secret Detection ๐Ÿ”ด

Patterns that indicate leaked credentials:

# Check staged files for secrets
git diff --cached --name-only | while read f; do
  echo "=== $f ==="
  git show ":$f" 2>/dev/null
done | grep -inE \
  'api[_-]?key|apikey|api[_-]?secret|\
secret[_-]?key|secret[_-]?token|\
auth[_-]?token|access[_-]?token|bearer[_-]?\
private[_-]?key|ssh[_-]?key|rsa[_-]?private|\
password\s*=\s*["\x27][^\x27"]{6,}|\
passwd\s*=\s*["\x27][^\x27"]{6,}|\
aws_access_key_id|aws_secret_access_key|\
AKIA[0-9A-Z]{16}|\
ghp_[a-zA-Z0-9]{36}|github_pat_|\
sk-[a-zA-Z0-9]{32,}|\
xoxb-|xoxa-|xoxp-|\
glpat-|glcpat-|\
npm_[a-zA-Z0-9]{36}|\
-----BEGIN (RSA|EC|DSA|OPENSSH) PRIVATE KEY'

High-risk literal patterns to check for:

# Check for raw high-entropy strings (possible tokens/keys)
git diff --cached | grep "^+" | grep -vE "^(\\+\\+\\+)" | \
  grep -E '[a-zA-Z0-9+/]{40,}={0,2}' | \
  grep -vE '(hash|sha|digest|checksum|fingerprint|base64|encoded|example|placeholder|YOUR_|REPLACE_|<.*>)' | \
  head -20

Common secret formats by provider:

ProviderPatternExample prefix
OpenAIsk-[a-zA-Z0-9]{48}sk-proj-...
Anthropicsk-ant-[a-zA-Z0-9-]{95}sk-ant-api03-...
GitHubghp_[a-zA-Z0-9]{36}ghp_abc...
AWSAKIA[A-Z0-9]{16}AKIAIOSFODNN7...
Google APIAIza[0-9A-Za-z-_]{35}AIzaSy...
Slackxoxb-[0-9-]{50,}xoxb-123-...
Stripesk_live_[a-zA-Z0-9]{24}sk_live_...
TwilioSK[a-zA-Z0-9]{32}SK1234...
JWTeyJ[a-zA-Z0-9-_]+\.[a-zA-Z0-9-_]+\.eyJhbGc...

Severity: ๐Ÿ”ด BLOCK โ€” never commit real credentials. If found:

  1. Remove the secret from staged files
  2. Rotate the credential immediately โ€” assume it's compromised
  3. Use environment variables, a secrets manager, or .env (gitignored)

Check 2: Sensitive File Detection ๐Ÿ”ด

# Check if sensitive file types are staged
git diff --cached --name-only | grep -iE \
  '\.(env|pem|key|p12|pfx|jks|keystore|ppk|ovpn)$|
  ^\.env(\.|$)|
  \.env\.(local|production|staging|dev|test)$|
  id_rsa|id_dsa|id_ecdsa|id_ed25519|
  \.ssh/|
  credentials$|credentials\.json|
  secrets\.json|secrets\.yaml|secrets\.yml|
  \.netrc$|
  wp-config\.php|
  database\.yml$|
  settings\/local\.py|
  config\/secrets\.'

Never commit these:

  • .env files with real values
  • Private key files (.pem, .key, .p12, .ppk)
  • SSH private keys (id_rsa, id_ed25519, etc.)
  • VPN configs (.ovpn)
  • credentials.json (Google service accounts)
  • secrets.yaml / secrets.yml

Severity: ๐Ÿ”ด BLOCK โ€” add to .gitignore immediately.


Check 3: Large Files ๐ŸŸก

# Find staged files over 1MB
git diff --cached --name-only | while read f; do
  size=$(git cat-file -s ":$f" 2>/dev/null || echo 0)
  if [ "$size" -gt 1048576 ]; then
    echo "LARGE: $f ($(( size / 1024 ))KB)"
  fi
done

Thresholds:

  • 1MB: ๐ŸŸก WARN โ€” is this intentional? Should it be in .gitignore or git-lfs?

  • 10MB: ๐Ÿ”ด BLOCK โ€” almost certainly wrong. Binary, dataset, or dependency artifact.

  • 50MB: ๐Ÿ”ด BLOCK โ€” will fail on GitHub/GitLab push limits.

Common large file mistakes:

  • node_modules/ committed by accident
  • Binary build artifacts (dist/, build/, *.pyc, *.class)
  • Datasets or fixtures that should be downloaded at runtime
  • Media files (images, video) that should use git-lfs or external storage

Check 4: Merge Conflict Markers ๐Ÿ”ด

# Detect unresolved merge conflicts in staged files
git diff --cached --name-only | while read f; do
  if git show ":$f" 2>/dev/null | grep -qE '^(<{7}|>{7}|={7}|[|]{7}) '; then
    echo "CONFLICT MARKERS: $f"
    git show ":$f" | grep -nE '^(<{7}|>{7}|={7}|[|]{7}) ' | head -5
  fi
done

Markers to detect:

  • <<<<<<< HEAD
  • =======
  • >>>>>>> branch-name
  • ||||||| merged common ancestors (diff3 style)

Severity: ๐Ÿ”ด BLOCK โ€” code with conflict markers will not compile or run.


Check 5: TODO/FIXME/HACK in New Code ๐Ÿ”ต

# Find new lines with quality flags in staged diff
git diff --cached | grep "^+" | grep -vE "^\+\+\+" | \
  grep -iE '\b(TODO|FIXME|HACK|XXX|BUG|TEMP|KLUDGE|NOCOMMIT)\b' | \
  head -20

Severity: ๐Ÿ”ต INFO โ€” not a blocker, but worth knowing what's going in.

Special case โ€” ๐Ÿ”ด BLOCK on NOCOMMIT:

git diff --cached | grep "^+" | grep -iE '\bNOCOMMIT\b'

If found, stop โ€” this was intentionally marked to not be committed.


Check 6: .gitignore Coverage Audit ๐ŸŸก

# Check if common sensitive paths are covered by .gitignore
cat .gitignore 2>/dev/null | sort > /tmp/gi_current.txt

echo "Checking .gitignore coverage..."

patterns=(
  "*.env"
  ".env"
  ".env.*"
  "*.pem"
  "*.key"
  "*.p12"
  "id_rsa"
  "id_ed25519"
  "*.log"
  "node_modules/"
  "__pycache__/"
  "*.pyc"
  "dist/"
  "build/"
  ".DS_Store"
  "Thumbs.db"
  "*.sqlite"
  "*.sqlite3"
  "venv/"
  ".venv/"
  "*.orig"
  "secrets.*"
  "credentials.json"
)

for p in "${patterns[@]}"; do
  if ! grep -qF "$p" .gitignore 2>/dev/null; then
    echo "MISSING: $p"
  fi
done

Severity: ๐ŸŸก WARN for missing patterns โ€” add them before the next commit touches those file types.

Recommended .gitignore additions for AI-assisted projects:

# Secrets & credentials
.env
.env.*
!.env.example
*.pem
*.key
*.p12
*.pfx
*.ppk
id_rsa
id_ed25519
id_ecdsa
credentials.json
secrets.yaml
secrets.yml
.netrc

# AI agent artifacts (if applicable)
.agent_memory/
agent_logs/
session_*.json

# Common build artifacts
node_modules/
dist/
build/
__pycache__/
*.pyc
*.pyo
.venv/
venv/
*.egg-info/

# OS
.DS_Store
Thumbs.db
*.orig

Check 7: Accidental Debug Code ๐Ÿ”ต

# Detect common debug artifacts in staged diff
git diff --cached | grep "^+" | grep -vE "^\+\+\+" | \
  grep -iE \
  'console\.log\(|print\(f?["\x27](debug|test|tmp|REMOVE|DELETE ME)|\
debugger;|\
pdb\.set_trace|breakpoint\(\)|\
binding\.pry|\
var_dump\(|die\(|exit\(1\)' | \
  head -20

Severity: ๐Ÿ”ต INFO โ€” common in AI-generated code. Review before merging to main.


Full Run Command

Run the complete suite against currently staged changes:

echo "๐Ÿ›ก๏ธ  Git Guardian โ€” Pre-Commit Safety Check"
echo "==========================================="
echo ""

# 1. What's staged?
echo "๐Ÿ“‹ Staged files:"
git diff --cached --name-only
echo ""

# 2. Run checks (see above for full implementations)
echo "๐Ÿ”ด Check 1: Secrets..."
echo "๐Ÿ”ด Check 2: Sensitive files..."
echo "๐ŸŸก Check 3: Large files..."
echo "๐Ÿ”ด Check 4: Merge conflict markers..."
echo "๐Ÿ”ต Check 5: TODO/FIXME/NOCOMMIT..."
echo "๐ŸŸก Check 6: .gitignore coverage..."
echo "๐Ÿ”ต Check 7: Debug artifacts..."

Output Format

๐Ÿ›ก๏ธ  Git Guardian โ€” Pre-Commit Safety Check
===========================================
Staged files: 4 | Checks: 7 | Time: 0.3s

๐Ÿ”ด BLOCKED (2 issues โ€” fix before committing)
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
[1] SECRET DETECTED in src/config.py (line 14)
    OPENAI_API_KEY = "sk-proj-abc123..."
    โ†’ Remove key, rotate immediately, use env var

[2] MERGE CONFLICT MARKERS in src/api/routes.py
    Line 47: <<<<<<< HEAD
    Line 52: >>>>>>> feature/auth-refactor
    โ†’ Resolve conflict before committing

๐ŸŸก WARNINGS (1 issue โ€” review recommended)
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
[3] LARGE FILE: data/fixtures.json (8.2MB)
    โ†’ Add to .gitignore or move to git-lfs

๐Ÿ”ต INFO (3 notes)
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
[4] TODO found in src/auth.py (line 88)
    # TODO: add rate limiting here
[5] console.log found in frontend/app.ts (line 23)
[6] .gitignore missing: *.pem, .env.*

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
โŒ COMMIT BLOCKED โ€” resolve 2 critical issue(s) first
๐Ÿ›ก๏ธ  Git Guardian โ€” Pre-Commit Safety Check
===========================================
Staged files: 3 | Checks: 7 | Time: 0.2s

โœ… All checks passed โ€” safe to commit

Installing as a Git Hook

To run automatically before every commit in a project:

cat > .git/hooks/pre-commit << 'EOF'
#!/bin/bash
# Git Guardian pre-commit hook
# Runs basic safety checks before allowing commit

# Check for secrets
if git diff --cached | grep -qiE 'api[_-]?key\s*=\s*["\x27][^\x27"]{10,}|AKIA[0-9A-Z]{16}|sk-[a-zA-Z0-9]{32,}|ghp_[a-zA-Z0-9]{36}'; then
  echo "๐Ÿ”ด Git Guardian: Possible secret detected in staged changes"
  echo "   Run a full check: ask your AI agent to run git-guardian"
  exit 1
fi

# Check for merge conflict markers
if git diff --cached | grep -qE '^(<{7}|>{7}|={7}) '; then
  echo "๐Ÿ”ด Git Guardian: Merge conflict markers detected"
  exit 1
fi

# Check for NOCOMMIT
if git diff --cached | grep -qiE '\bNOCOMMIT\b'; then
  echo "๐Ÿ”ด Git Guardian: NOCOMMIT marker found โ€” this change was flagged to not be committed"
  exit 1
fi

echo "โœ… Git Guardian: Basic checks passed"
exit 0
EOF

chmod +x .git/hooks/pre-commit
echo "โœ… Git Guardian pre-commit hook installed"

Why This Matters for AI-Assisted Development

AI coding tools are fast โ€” sometimes too fast. Common failure modes:

  1. Context leakage โ€” agent reads a .env file for context, then writes the values into generated code
  2. Conflict confusion โ€” agent sees conflict markers and treats them as code, writes around them instead of resolving
  3. Overeager staging โ€” git add . after AI-generated files includes things that should be ignored
  4. Debug trails โ€” AI includes console.log, print, breakpoint() for its own reasoning, forgets to remove them
  5. Fixture bloat โ€” AI generates large test fixtures inline instead of loading from external source

Git Guardian is your last line of defense before those mistakes become permanent history.

Source

git clone https://github.com/suryast/free-ai-agent-skills/blob/main/git-guardian/SKILL.mdView on GitHub

Overview

Git Guardian provides pre-commit safety checks for AI-assisted development. It scans staged changes for secrets, sensitive files, merge-conflict markers, and large files, helping prevent credential leakage and messy histories before they reach your repo. By catching AI coding mistakes early, it keeps code safer and cleaner.

How This Skill Works

It analyzes staged changes using git diff --cached and reports findings with severity levels BLOCK, WARN, and INFO. The full check suite includes secret detection, sensitive file detection, and large file checks, surfacing issues before commits are made.

When to Use It

  • Before committing AI-generated code
  • After a large AI-generated code dump
  • Setting up a new repo to enforce safety
  • Before pushing changes to shared branches
  • Regularly in CI to catch issues before merge

Quick Start

  1. Step 1: Install or enable the git-guardian pre-commit checks in your repo
  2. Step 2: Stage changes and run git commit to trigger the checks
  3. Step 3: Review BLOCK/WARN/INFO reports and fix issues before re-trying commit

Best Practices

  • Integrate git-guardian as a pre-commit hook in your repo
  • Keep secret patterns and provider examples up to date in the rules
  • Whitelisting safe files to reduce false positives
  • Rotate credentials immediately if a secret is detected
  • Use environment variables or a secrets manager instead of hardcoding values

Example Use Cases

  • A developer attempts to commit a code snippet containing an API key and the commit is blocked.
  • Staged files include a private key or PEM file and are flagged as sensitive.
  • An AI-generated dump introduces a large binary; a large-file check issues a warning.
  • Merge conflict markers appear in staged changes and are detected before commit.
  • A credential string matching a known provider pattern is detected and blocked.

Frequently Asked Questions

Add this skill to your agents

Related Skills

Sponsor this space

Reach thousands of developers โ†—