Smart Guardrail System for Safer Claude Code Development
๐ฅ If this project helps you, please click the โญ Star in the top right corner to support us!
Have you ever encountered these frustrating scenarios while Vibe Coding?
โ Direct modification on main branch โ One mistake, important code gets overwritten
โ Interrupted modifications โ Intermediate artifacts lost, unable to rollback to specific states
โ Chaotic multiple modifications โ Don't know which change introduced the bug
โ Accidentally deleted important files โ No timely commits, game over
โ Manual changes overwritten โ AI directly overwrites uncommitted manual changes, cannot distinguish code ownership
Guardrails based on Claude Code Hook
Triple Security Mechanism: ๐ก๏ธ Pre-write Interception Alert + ๐ธ Precise Snapshot Commits + ๐ Smart Merge Archiving
โ
Protected branch interception โ Automatically blocks writes to main branch, suggests creating feature branch
โ
Uncommitted changes protection โ Blocks when manual changes are uncommitted, forces commit first to avoid overwriting
โ
File-level precise snapshots โ Each modification only commits changed files, avoiding noise
โ
Smart backup mechanism โ Automatic backup before gitignore file modifications
โ
Safe history merging โ Multiple checkpoints intelligently merged into clean task commits
|
|
|
|
- โ
Don't overwrite existing Claude Code Hooks - Intelligently merge
.claude/settings.json
- โ Backup + Deduplication + Idempotent - Safe even with multiple installations
- โ Coexist with user hooks - Don't affect existing workflows
Solution | Traditional Approach | Manual Backup | ๐ก๏ธ Guardrails |
---|---|---|---|
Branch Protection | โ Rely on human memory | โ Remedial action after the fact | โ Automatic interception alerts |
Version Tracking | โ Lose intermediate states | โ Precise automatic snapshots | |
History Organization | โ Messy commit records | โ Cannot trace back | โ Smart merge archiving |
Ease of Use | โ Cumbersome and error-prone | โ One-click install and use |
-
Git installed and initialized (configured with
user.name
/user.email
) -
Claude Code installed (version supporting Hooks and
PreToolUse exit=2
blocking) -
jq (for parsing Hook input JSON)
- macOS:
brew install jq
- Ubuntu:
sudo apt-get install -y jq
- macOS:
Smart merge, safe and worry-free # Project-level installation
curl -fsSL https://raw.githubusercontent.com/wangbooth/claude-code-guardrails/main/install.sh | bash # Global installation
curl -fsSL https://raw.githubusercontent.com/wangbooth/claude-code-guardrails/main/install.sh | bash -s -- --global |
Intuitive and transparent, complete control cp -r .claude/ /path/to/your-project/
chmod +x /path/to/your-project/.claude/hooks/guardrails/*.sh |
๐ Smart merge - Don't overwrite existing configurations
๐พ Automatic backup - Backup settings.json before changes
๐ Idempotent safety - Multiple runs have no side effects
๐งน Deduplication - Avoid duplicate configuration items
# Project-level uninstall
curl -fsSL https://raw.githubusercontent.com/wangbooth/claude-code-guardrails/main/uninstall.sh | bash
# Keep script files, only remove from settings.json
curl -fsSL https://raw.githubusercontent.com/wangbooth/claude-code-guardrails/main/uninstall.sh | bash -s -- --keep-scripts
# Global uninstall
curl -fsSL https://raw.githubusercontent.com/wangbooth/claude-code-guardrails/main/uninstall.sh | bash -s -- --global
.
โโ .claude/
โ โโ settings.json # Hook entry and matching rules (template)
โ โโ hooks/
โ โโ guardrails/
โ โ โโ guard-branch.sh # Pre-write branch guard (protected branch blocking)
โ โ โโ auto-commit.sh # Per-modification precise snapshot commits
โ โ โโ squash-checkpoints.sh # Stop/Compact stage safe merging
โโ install.sh # One-click installation (merge/backup/dedup/idempotent)
โโ uninstall.sh # One-click uninstall (can keep scripts)
โโ README.md
โโ LICENSE # MIT
graph LR
A[๐ Claude Code attempts write] --> B{๐ก๏ธ Branch check}
B -->|Protected branch| C[โ Block and alert]
C --> D[๐ก Suggest creating vibe branch]
B -->|Safe branch| E{๐ Uncommitted check}
E -->|Has uncommitted| F[โ Force commit first]
F --> G[๐พ Prompt to save manual changes]
E -->|No uncommitted| H[โ
Allow write]
H --> I[๐ธ Auto checkpoint]
I --> J[๐พ Precisely commit changed files]
J --> K[๐ Session ends]
K --> L[๐ฆ Smart merge into task commit]
style B fill:#ff9999
style E fill:#ffcc99
style I fill:#99ff99
style L fill:#9999ff
Attempting to write on protected branch โ Blocked and alerted, terminal suggests creating vibe
branch:
โ ๏ธ Branch 'main' is protected.
Current branch 'main' is protected.
It's recommended to create/switch to a vibe branch before making changes:
ๅปบ่ฎฎๅๅปบ/ๅๆขๅฐ vibe ๅๆฏๅไฟฎๆน๏ผไพๅฆ๏ผ
git checkout -b vibe/20250811142530-claude
Detected uncommitted manual changes โ Blocked and prompted to commit first:
โ ๏ธ Detected uncommitted changes in the repository.
ๆฃๆตๅฐไปๅบไธญๆๆชๆไบค็ๆดๆนใ
There are uncommitted changes that may be lost if AI overwrites them.
ๅญๅจๆชๆไบค็ๆดๆน๏ผๅฆๆ่ขซ AI ่ฆ็ๅฏ่ฝๅฏผ่ดไปฃ็ ไธขๅคฑใ
Consider committing these changes first:
ๅปบ่ฎฎๅ
ๆไบค่ฟไบๆดๆน๏ผ
git add .
git commit -m "Save work in progress"
Switch to vibe branch and continue working โ Auto checkpoint after each Write/Edit/MultiEdit
:
checkpoint: Write src/LoginForm.tsx - 14:25
checkpoint: Edit src/types.ts - 14:26
If modifying gitignore files, you'll receive backup reminders:
๐ฆ Backup Notice: '.env' is a gitignored file. A backup has been saved to '.claude/backups/.env.20250811-142530' before claude code modification.
๐ฆ ๅคไปฝๆ้: '.env' ๆฏ gitignore ๆไปถ๏ผclaude code ไฟฎๆนๅๅทฒๅคไปฝๅฐ '.claude/backups/.env.20250811-142530'
End session stop
/ trigger compact
โ Automatically merge multiple checkpoints into one task commit:
task: modify LoginForm.tsx, types.ts - 2025-08-11
- Branch Protection: Default protection for
main|master|dev|release*
, adjustable inguard-branch.sh
. - Don't break public history: No squash on protected branches; only merge checkpoints when "no upstream or ahead>0".
- Collaboration recommendation: More stable when combined with remote branch protection (no force push/requires PR).
- Commit message conventions: Change
auto-commit.sh
tochore(checkpoint): ...
; Stop stage outputsvibe/fix/...
types (Conventional Commits). - Stricter write paths: To completely prevent "direct file write tools", only allow custom MCP write tools in
PreToolUse
, block genericWrite|Edit|MultiEdit
. - External integration: Call your CLI/API in
PostToolUse
orStop
(tickets, PRs, message notifications, etc.).
Q: Will it overwrite my existing hooks?
A: No. The installation script uses jq
to merge .claude/settings.json
, only appends and deduplicates for the same matcher
; and generates .bak.*
backup before writing back.
Q: If I manually modified code but haven't committed yet, can Claude Code still continue working?
A: No. guard-branch.sh will detect uncommitted changes and block the operation, prompting you to commit first. This prevents AI from overwriting your manual modifications, ensuring clear and traceable code ownership.
Q: Will multiple hooks execute in order?
A: Multiple hooks matched by the same event execute in parallel; if strict ordering is needed, write multiple commands in the same script for serial execution.
Q: Does it support Windows?
A: Scripts are based on POSIX shell. Windows users are recommended to use WSL or Unix-like environments.
โก Triple action: Star โญ + Fork ๐ด + Share ๐ข
๐ Found a Bug
|
๐ก Feature Suggestion
|
๐ Code Contribution
|
This project is licensed under the MIT License, see LICENSE file for details
โญ If this project helps you, please click Star to support us! โญ
๐ก๏ธ Chase the vibe, but honor the craft. ๐ก๏ธ