Loading lesson…
An agent went off-script, broke your build, and committed garbage. Learn the systematic recovery workflow — git, sanity checks, and the cultural habits that make recovery fast.
An agent has just made 47 edits across 12 files, run `git push --force` to your branch, and now nothing builds. This will happen to you at least once. Here is the recovery procedure that turns a five-hour disaster into a fifteen-minute lesson.
# What changed in the working tree (uncommitted)
git status --short
# What got committed during the session
git log --oneline -20
# What got pushed (if remote was touched)
git log origin/main..HEAD --oneline
# Files modified, sorted by churn
git diff --stat HEAD~10..HEAD | sort -k3 -n -r | head -20
# Reflog — the safety net for everything git
git reflog --date=iso | head -30Five commands that paint a complete picture before you touch anything.Even if the agent ran `reset --hard`, `rebase`, or `push --force`, git reflog has the previous state for at least 30 days. Every commit your repo has ever pointed at is recoverable — as long as you don't run `git gc --prune=now` first.
# Find the commit hash from before the agent disaster
git reflog
# 7a3f2c8 HEAD@{0}: reset: moving to HEAD~5
# b9c1d4e HEAD@{1}: commit: agent: refactor everything (this is the chaos)
# 4f5a6b7 HEAD@{2}: checkout: moving from main to feature <-- known good
# Recover to known good in a new branch (safer than overwriting)
git branch recovery 4f5a6b7
git checkout recovery
# Or, if you're sure: reset main
git checkout main
git reset --hard 4f5a6b7Reflog references survive reset, rebase, and force-push. They are the most underrated git feature.| Scope | What it touched | Action |
|---|---|---|
| Working tree only | Uncommitted edits | `git stash` or `git restore .` — done in 5 seconds |
| Local commits, not pushed | 1-N local commits | `git reset --hard <good-sha>` — minute or two |
| Pushed to feature branch | Force-pushed PR | Find via reflog, force-push the recovered tree |
| Pushed to main / production | Other people now have it | Coordinate with team, revert via PR (do not force-push main) |
# Future-proofing: never run an agent on main
1. Always work on a branch:
git checkout -b agent/refactor-auth
2. Set Claude Code / Cursor permissions:
- No git push
- No `rm -rf` outside cwd
- No edits in protected paths (.github/, secrets/, migrations/)
3. Use a worktree for risky agent runs:
git worktree add ../experiment HEAD
cd ../experiment
# If it goes wrong: rm -rf ../experiment, no harm to main repo
4. CI must pass before merge — agent can't commit to main directlyThese four rules, applied at the team level, eliminate most agent disasters before they start.Once everything is green again, write a 5-minute blameless postmortem to yourself. What permission did the agent have that it shouldn't? What signal would have stopped you sooner? What do you change in your CLAUDE.md, .cursor/rules, or pre-commit hooks to prevent this exact failure? Save it. Read it next time you set up a new project.
The agents will mess up. Plan to recover, not to prevent.
— A platform engineer who has seen things
The big idea: agent disasters are inevitable; data loss is not. Branch, sandbox, and trust git reflog. When the agent goes off the rails, stop, inventory, recover from reflog, and turn the postmortem into a permission rule that prevents the exact failure. Recovery is a skill, not a panic.
15 questions · take it digitally for instant feedback at tendril.neural-forge.io/learn/quiz/end-coding-debug-recovery-bad-agent-creators
What is the absolute FIRST action you should take when an AI agent makes dozens of edits across many files and the build suddenly breaks?
Why should you avoid running git pull, git fetch, or git checkout immediately after discovering an agent has damaged the repository?
What does git reflog actually store and preserve?
An agent runs `git reset --hard` to an old commit and then executes `git push --force`. Even after this destructive sequence, what provides a path to recover the lost commits?
In the context of assessing damage from an out-of-control agent, what does 'blast radius' refer to?
When the damage is limited to uncommitted edits in the working tree only, what is the fastest recovery method?
If an agent created several local commits that have NOT yet been pushed to any remote, what is the recommended recovery approach?
Why is force-pushing to the main or production branch generally prohibited in team environments?
An agent has deployed broken code to a production environment. What is the correct recovery procedure?
What git command restores a single specific file to its state from N commits ago?
What additional recovery option do VS Code Timeline and JetBrains Local History provide beyond git?
According to the cultural practices described, when should a blameless postmortem be written?
What specific elements should a blameless postmortem identify to prevent future incidents?
The lesson quotes: 'The agents will mess up. Plan to recover, not to prevent.' What underlying principle does this express?
What does the lesson identify as the 'big idea' - the core principle that agent disasters are inevitable but data loss is not?