Emergency Procedures
Module 8.5: Emergency Procedures
Section titled “Module 8.5: Emergency Procedures”Estimated time: ~30 minutes
Prerequisite: Module 8.4 (Quality Assessment)
Outcome: After this module, you will have a mental playbook for Claude Code emergencies, know recovery commands by heart, and be able to act quickly when things go wrong.
1. WHY — Why This Matters
Section titled “1. WHY — Why This Matters”Claude was supposed to “clean up the config files.” You approved without looking carefully. Now your .env file is deleted, production environment variables are gone, and you’re scrambling to remember what was in there.
Or: Claude modified 50 files in a “refactor” and you have no idea what actually changed.
Emergencies happen. Even with all the safeguards from earlier modules. The question is: do you have a recovery plan? The worst time to figure out emergency procedures is DURING an emergency.
2. CONCEPT — Core Ideas
Section titled “2. CONCEPT — Core Ideas”Emergency Severity Levels
Section titled “Emergency Severity Levels”| Level | Situation | Response Time | Example |
|---|---|---|---|
| 🔴 Critical | Production affected, data loss | Immediate | Deleted .env, broke production |
| 🟠 Major | Development blocked | Minutes | 50 files modified, can’t continue |
| 🟡 Minor | Confused state, recoverable | When convenient | Context confusion, stuck loop |
The Emergency Playbook
Section titled “The Emergency Playbook”Memorize this sequence:
- STOP: Ctrl+C immediately. Don’t let Claude continue.
- ASSESS:
git status+git diff— what actually changed? - CONTAIN:
git stash— save current state before recovering - RECOVER: Choose recovery strategy based on severity
- DOCUMENT: What went wrong? Update CLAUDE.md to prevent recurrence.
Recovery Strategies
Section titled “Recovery Strategies”| Strategy | Command | When to Use |
|---|---|---|
| Discard one file | git checkout <file> | One file is wrong |
| Discard all changes | git checkout . | Everything since last commit is bad |
| Hard reset | git reset --hard HEAD | Complete disaster recovery |
| Recover deleted commits | git reflog | If you reset too hard |
| Start fresh session | /clear | Claude context is hopelessly confused |
Pre-Emergency Preparation
Section titled “Pre-Emergency Preparation”- Commit frequently (small commits = easy recovery points)
- Use feature branches (isolate AI work)
- Backup .env and sensitive files outside git
- Know your recovery commands by heart
- Never Full Auto without git safety net
3. DEMO — Step by Step
Section titled “3. DEMO — Step by Step”Scenario 1: Claude Deleted Important Files
Section titled “Scenario 1: Claude Deleted Important Files”STOP — See Claude deleting files? Press Ctrl+C immediately.
ASSESS:
$ git statusExpected output:
Changes not staged for commit: deleted: .env deleted: config/production.json modified: src/config.tsCONTAIN:
$ git stashExpected output:
Saved working directory and index state WIP on main: abc1234 Last commitRECOVER:
$ git checkout .Expected output:
Updated 3 paths from the indexVerify recovery:
$ ls .env config/production.jsonExpected output:
.env config/production.jsonFiles are back.
Scenario 2: Claude Modified 50 Files
Section titled “Scenario 2: Claude Modified 50 Files”STOP: Ctrl+C
ASSESS:
$ git diff --statExpected output:
50 files changed, 2000 insertions(+), 500 deletions(-)$ git diff --name-onlyExpected output:
src/api/users.tssrc/api/products.ts... (48 more files)CONTAIN:
$ git stashPARTIAL RECOVERY (if some changes were good):
$ git stash pop$ git checkout src/unrelated/$ git add src/feature/$ git commit -m "Partial work from AI session"NUCLEAR RECOVERY (if everything is bad):
$ git reset --hard HEADScenario 3: Reset Too Hard, Lost Work
Section titled “Scenario 3: Reset Too Hard, Lost Work”$ git reflogExpected output:
abc1234 HEAD@{0}: reset: moving to HEADdef5678 HEAD@{1}: commit: My work before disasterghi9012 HEAD@{2}: commit: Earlier workRecover:
$ git reset --hard def5678Your work is back.
4. PRACTICE — Try It Yourself
Section titled “4. PRACTICE — Try It Yourself”Exercise 1: Emergency Drill
Section titled “Exercise 1: Emergency Drill”Goal: Practice the full emergency playbook in a safe environment.
Instructions:
- Create a test repository with some files
- Make intentional “bad” changes (delete a file, modify several)
- Practice: STOP → ASSESS → CONTAIN → RECOVER
- Time yourself. Target: full recovery in under 2 minutes.
💡 Hint
Setup:
mkdir emergency-drill && cd emergency-drillgit initecho "important" > config.txtecho "SECRET=abc123" > .envgit add . && git commit -m "Initial"
# Simulate disasterrm .envecho "broken" >> config.txtNow practice recovery.
Exercise 2: Recovery Command Muscle Memory
Section titled “Exercise 2: Recovery Command Muscle Memory”Goal: Make recovery commands automatic.
Practice until you can type without thinking:
git status # What changed?git diff # What exactly?git stash # Save stategit checkout . # Discard allgit checkout <file> # Discard onegit reset --hard HEAD # Nucleargit reflog # Find lost commitsExercise 3: Post-Mortem Practice
Section titled “Exercise 3: Post-Mortem Practice”Goal: Build the documentation habit.
Instructions:
- Simulate an emergency (Exercise 1)
- After recovery, write a brief post-mortem:
- What happened?
- Why did it happen?
- How to prevent next time?
- Draft a CLAUDE.md addition to prevent recurrence
✅ Solution
Example post-mortem:
What happened: Claude deleted .env while “cleaning up config”
Why: Vague prompt (“clean up”) + approved without reviewing
Prevention: Add to CLAUDE.md:
## Dangerous OperationsNEVER delete without explicit approval:- .env files- config/*.json- Migration files5. CHEAT SHEET
Section titled “5. CHEAT SHEET”Emergency Playbook
Section titled “Emergency Playbook”- 🛑 STOP: Ctrl+C
- 🔍 ASSESS:
git status+git diff - 📦 CONTAIN:
git stash - 🔧 RECOVER: See commands below
- 📝 DOCUMENT: Update CLAUDE.md
Recovery Commands
Section titled “Recovery Commands”# See damagegit status && git diff --stat
# Save mess before recoveringgit stash
# Undo one filegit checkout path/to/file
# Undo everythinggit checkout .
# Nuclear resetgit reset --hard HEAD
# Recover from bad resetgit refloggit reset --hard <commit-hash>Prevention Checklist
Section titled “Prevention Checklist”- Commit before AI sessions
- Use feature branches
- Never Full Auto without git branch
- Backup .env files separately
6. PITFALLS — Common Mistakes
Section titled “6. PITFALLS — Common Mistakes”| ❌ Mistake | ✅ Correct Approach |
|---|---|
| Panicking and running commands randomly | Follow playbook: STOP → ASSESS → CONTAIN → RECOVER |
git reset --hard as first response | Assess first. Sometimes partial recovery is better. |
Forgetting git stash before recovery | Always stash first. You might need to inspect the bad state. |
| Not knowing reflog exists | git reflog can recover almost anything. Learn it. |
| Same emergency twice | Document and update CLAUDE.md after every emergency |
| No commits before AI sessions | Clean commit = clean recovery point. Non-negotiable. |
| Keeping .env only in working directory | Backup sensitive files outside git separately |
7. REAL CASE — Production Story
Section titled “7. REAL CASE — Production Story”Scenario: Vietnamese startup, Friday 6pm. Dev was rushing to finish a feature, used Full Auto to “clean up and refactor.” Went to get coffee. Came back to find Claude had deleted 3 migration files it considered “outdated” and modified the database schema.
Panic response (wrong):
- Tried to recreate migration files from memory
- Ran migrations on staging — broke everything
- Spent 4 hours trying to recover database
What should have happened:
- STOP: Ctrl+C (or just don’t approve the deletion)
- ASSESS:
git diff --statwould have shown migration deletions - CONTAIN:
git stash - RECOVER:
git checkout db/migrations/ - DOCUMENT: Add to CLAUDE.md: “NEVER delete migration files without explicit approval”
Lesson learned: “2 minutes of emergency procedure saves 4 hours of panic. We now have emergency commands printed and taped to monitors.”
Phase 8 Complete! You can now debug Claude itself — detecting hallucinations, breaking loops, fixing context confusion, assessing quality, and recovering from emergencies.
Next Phase: Phase 9: Legacy Code & Brownfield — Apply Claude Code to existing codebases.