Skip to content

Protective hooks for Claude Code that prevent accidental code loss through branch protection, automatic checkpointing, and safe commit squashing

License

Notifications You must be signed in to change notification settings

wangbooth/Claude-Code-Guardrails

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

10 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ›ก๏ธ Claude Code Guardrails

Smart Guardrail System for Safer Claude Code Development

GitHub Stars GitHub Forks License Issues

๐Ÿ”ฅ If this project helps you, please click the โญ Star in the top right corner to support us!

๐Ÿ‡บ๐Ÿ‡ธ English | ๐Ÿ‡จ๐Ÿ‡ณ ไธญๆ–‡


๐Ÿšจ Pain Points: Claude Code's "Dangerous Moments"

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

๐Ÿ’ก Solution: Claude Code Guardrails

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

๐ŸŽ‰ Start experiencing now!

Install Now ย ย  Click Star


โœจ Core Features

๐Ÿ›ก๏ธ Dual Security Protection

  • Protected branch interception
  • Uncommitted changes detection
  • Pre-write blocking alerts
  • Smart suggestion to create vibe branch
  • Force commit manual changes before continuing

๐Ÿ“ธ Precise Snapshots

  • File-level snapshot commits
  • Avoid noise from git add .
  • Only commit currently changed files
  • Preserve complete modification history

๐Ÿ”„ Smart Backup

  • Automatic gitignore file backup
  • Backup to .claude/backups/ before modifications
  • Bilingual friendly reminders
  • Prevent loss of important config files

๐Ÿ“ฆ History Organization

  • Safe squash merging
  • Multiple checkpoints merged into tasks
  • Don't break protected branch history
  • Keep Git history clean

๐Ÿ”ง Perfect Compatibility

  • โœ… 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

๐Ÿ†š Compare with Other Solutions

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 โš ๏ธ Manual copy-paste โœ… Precise automatic snapshots
History Organization โŒ Messy commit records โŒ Cannot trace back โœ… Smart merge archiving
Ease of Use โš ๏ธ Requires experience โŒ Cumbersome and error-prone โœ… One-click install and use

๐Ÿงฐ Prerequisites

  • 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

๐Ÿš€ Quick Start

๐ŸŽฏ Choose Installation Method

๐ŸŒŸ Recommended: One-Click Installation

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

๐Ÿ“ Method 2: Manual Copy

Intuitive and transparent, complete control

cp -r .claude/ /path/to/your-project/
chmod +x /path/to/your-project/.claude/hooks/guardrails/*.sh

โœจ Installation Script Features

๐Ÿ”„ 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


๐Ÿ—‘๏ธ Uninstallation

# 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

๐Ÿ—‚๏ธ Directory Structure

.
โ”œโ”€ .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

๐Ÿ“บ Effect Demo

๐ŸŽฌ Workflow Overview

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
Loading

๐Ÿงช Usage Examples

Scenario 1: Protected Branch Interception

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

Scenario 2: Uncommitted Changes Protection

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"

Scenario 3: Normal Workflow

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'

Scenario 4: Session End Organization

End session stop / trigger compact โ†’ Automatically merge multiple checkpoints into one task commit:

task: modify LoginForm.tsx, types.ts - 2025-08-11

๐Ÿ›ก๏ธ Security Strategy and Best Practices

  • Branch Protection: Default protection for main|master|dev|release*, adjustable in guard-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).

๐Ÿ”ง Customization Guide

  • Commit message conventions: Change auto-commit.sh to chore(checkpoint): ...; Stop stage outputs vibe/fix/... types (Conventional Commits).
  • Stricter write paths: To completely prevent "direct file write tools", only allow custom MCP write tools in PreToolUse, block generic Write|Edit|MultiEdit.
  • External integration: Call your CLI/API in PostToolUse or Stop (tickets, PRs, message notifications, etc.).

โ“ Frequently Asked Questions (FAQ)

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.


๐ŸŒŸ Community Support

๐Ÿ’ Find it useful? Please support us!

Star this repo ย ย  Fork and contribute

โšก Triple action: Star โญ + Fork ๐Ÿด + Share ๐Ÿ“ข

๐Ÿค Contributing

๐Ÿ› Found a Bug
Submit Issue

๐Ÿ’ก Feature Suggestion
Feature Request

๐Ÿ”€ Code Contribution
Submit PR

๐Ÿ“Š Project Stats

Contributors Last Commit Commit Activity


๐Ÿ“„ License

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. ๐Ÿ›ก๏ธ

About

Protective hooks for Claude Code that prevent accidental code loss through branch protection, automatic checkpointing, and safe commit squashing

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages