# Git Undo Button for AI Coding (free AI agent kit from Bot Pantry)

Canonical page: https://botpantry.com/kits/git-safety-net
Free download (.zip, no sign-up): https://botpantry.com/api/download?kit=git-safety-net
For: Vibe coders letting AI edit their code (Cursor, Claude Code, v0, Lovable, Replit, Bolt)
Setup time: 20-50 min (verified by Kit Inspector)
Works with: Git; GitHub; Vercel; Cursor; Claude Code; Lovable; Replit; Bolt; v0; gitleaks (optional)
Published: 2026-11-23

Always get back to a working version when an AI edit goes wrong: tested checkpoint and undo scripts that never lose work, a pre-commit hook that blocks .env files and API keys, AI rules that forbid force-pushes and resets, and rescue playbooks for reflog, leaked keys and Vercel rollbacks.

## What's inside
- checkpoint and undo-last scripts (bash + PowerShell): undo uncommitted or committed AI changes, with every undone file copied to a backup branch first
- A pre-commit hook that blocks .env and key files plus Stripe, OpenAI, Anthropic, Supabase, AWS, GitHub and other key patterns, without printing the key (runs gitleaks too if installed)
- Rules for Cursor (.mdc + Auto-review permissions), Claude Code (CLAUDE.md + permission prompts) and AGENTS.md: checkpoint first, never force-push, reset, clean or rewrite history without asking
- Rescue playbooks: undo the last AI change, recover a deleted file, reflog after a bad reset, 'I pushed a secret' (rotate first), Vercel Instant Rollback
- What Cursor, Claude Code, Lovable, Replit and Bolt checkpoints restore, and what they can't (databases, deploys, emails)
- 10 copy-paste rescue prompts that make the AI preview and wait for your yes

---

The kit's README (included in the .zip) follows.

# Git Undo Button for AI Coding — Bot Pantry Kit

An undo button for when you let AI edit your code. Works with Cursor, Claude Code, v0, Lovable, Replit and Bolt projects that live in git. Scripts come in bash (macOS, Linux, Git Bash on Windows) and PowerShell versions. After about 20–50 minutes of setup (beginner, including installing Git), you can:

1. **Always get back to a working version.** `checkpoint.sh` saves everything as a git commit in one command. `undo-last.sh` undoes the last AI change, committed or not, and copies everything it undoes to a backup branch first, so no work is ever lost.
2. **Keep keys out of git.** A pre-commit hook blocks `.env` files and common API key formats before they're committed, without printing the key. Your AI gets rules that forbid committing secrets or skipping the hook.
3. **Stop the AI wrecking your history.** Rules for Cursor, Claude Code and AGENTS.md make it checkpoint before each task and ask you before any push, force-push, `reset --hard`, `clean`, history rewrite or branch delete.
4. **Recover when it goes wrong anyway:** reflog rescues, deleted files, "I pushed a secret" (rotate first), and rolling back a bad Vercel deploy.

## Contents
| File | What it is |
|---|---|
| `safety-net/checkpoint.sh`, `safety-net/checkpoint.ps1` | Save a checkpoint: stage everything except secrets files, scan, commit "checkpoint: …" |
| `safety-net/undo-last.sh`, `safety-net/undo-last.ps1` | Undo the last change. Preview by default; `--yes` / `-Yes` to act; `--to <hash>` / `-To` to go back further |
| `safety-net/install.sh`, `safety-net/install.ps1` | Install the hook, merge the `.gitignore` starter into yours (append only), keep the scripts LF in `.gitattributes` |
| `safety-net/pre-commit` | The secret-blocking git hook (POSIX sh; Git for Windows runs it too) |
| `safety-net/gitignore-starter.txt` | `.gitignore` lines for Next.js, Vite, Supabase CLI, Vercel and secrets files |
| `drop-in/AGENTS.md`, `drop-in/CLAUDE.md`, `drop-in/.cursor/rules/git-safety-net.mdc` | Ready-to-save AI rules |
| `drop-in/.claude/settings.json`, `drop-in/.cursor/permissions.json` | Optional: Claude Code asks before risky git commands; Cursor Auto-review guidance |
| `playbooks/01-undo-last-ai-change.md` | Undo the last AI change (uncommitted, committed, several back, one file, merges) |
| `playbooks/02-recover-deleted-file.md` | Bring back a deleted file or folder |
| `playbooks/03-bad-reset-reflog.md` | After `reset --hard`, a bad rebase or a deleted branch: the reflog |
| `playbooks/04-pushed-a-secret.md` | Leaked a key: rotate first, then untrack; push protection; optional history rewrite |
| `playbooks/05-vercel-rollback.md` | Instant Rollback, promoting a deployment, preview deployments |
| `playbooks/06-tool-checkpoints.md` | Cursor, Claude Code, Lovable, Replit, Bolt checkpoints and their limits |
| `prompts.md` | 10 copy-paste rescue prompts that make the AI preview first and wait for your yes |
| `setup/01-install-git.md` … `setup/05-ai-tools.md` | Git, your project, GitHub, Vercel, where the rules go in each tool |
| `checklist.md`, `guardrails.md`, `examples.md`, `troubleshooting.md`, `sources.md` | Checklist, what it does and doesn't protect, real output, fixes, doc links |

## What you need
- **Git 2.36 or newer.** Windows: Git for Windows (includes Git Bash). Free.
- A project folder you edit with an AI tool. Browser-only Lovable/Bolt/Replit projects: connect GitHub and clone them first (setup/01-install-git.md (in the kit .zip)).
- Optional: a GitHub account (free), a Vercel project, [gitleaks](https://github.com/gitleaks/gitleaks).
- No accounts, installs or paid plans are needed for the scripts themselves.

## Setup (20–50 minutes the first time)
1. **Git** (5–15 min, 0 if installed): setup/01-install-git.md (in the kit .zip).
2. **Copy `safety-net/` and the rules into your project**, run `bash safety-net/install.sh`, save your first checkpoint, and prove the hook blocks a fake `.env` file (10–15 min): setup/02-add-to-your-project.md (in the kit .zip).
3. **GitHub** push protection and secret scanning, what's on by default (5–10 min, optional): setup/03-github.md (in the kit .zip).
4. **Vercel**: find Instant Rollback before you need it (5 min, optional): setup/04-vercel.md (in the kit .zip).
5. **Your AI tool**: which file each tool reads (2–5 min): setup/05-ai-tools.md (in the kit .zip).

Windows PowerShell users: every `bash safety-net/X.sh` has a twin, `powershell -ExecutionPolicy Bypass -File safety-net\X.ps1`. Flags are `-Yes` and `-To <hash>`.

## How to use it
```bash
bash safety-net/checkpoint.sh "pricing page works"   # before each AI task, and when it works
bash safety-net/undo-last.sh                         # something broke: see what undo would do
bash safety-net/undo-last.sh --yes                   # do it (backup branch made first)
bash safety-net/undo-last.sh --to 3f2a1bc --yes      # go back to an older checkpoint
```
Your AI runs `checkpoint.sh` itself once the rules are in place.

**How undo-last decides:**
- **Uncommitted changes:** they're copied to `safety-backup/undo-<time>` and checked file by file, then your files go back to the last checkpoint.
- **No uncommitted changes:** the last commit is undone with `git revert`, a new commit, so history is kept and it's safe after a push. A `safety-backup/before-revert-<time>` branch marks where you were.
- **Never touched:** `.env`/key files and the `safety-net/` folder.
- **Refuses (changing nothing):** a merge commit, your very first commit, a detached HEAD, a merge/rebase in progress, or an undo that would put a removed secret back.

## Guardrails
- Scripts never push, `reset --hard`, `clean`, rebase, amend, skip hooks, or delete branches.
- Rotate a leaked key first; rewriting git history is secondary and often unnecessary (GitHub's guidance too).
- The hook catches common patterns, not every secret. `--no-verify` skips it, and it only exists where you ran `install.sh`.
- Rules files and permission prompts guide the AI but aren't locks.
- Git, and every AI tool's checkpoints, restore **files**. They don't undo database changes, migrations, emails, payments, env vars on Vercel, or live deploys.
- Backup branches stay on your computer. Don't push them.

Details: guardrails.md (in the kit .zip).

## Tested
Everything ran on the Bot Pantry build box on 2026-11-27 with **fake keys only**, in throwaway git repos (Git 2.47.3, Linux). Script copies had their execute bits removed to match an unzipped download. **282 checks, 0 failures, 0 skipped** (this run: 280 passed, 1 skipped — `git-filter-repo` was not installed; the two history-rewrite checks are included in the 282):

- **Hook, 45 checks.** It blocked 11 secrets file names (`.env`, `.env.local`, `.env.production`, a nested `.env.development.local`, `.envrc`, `id_rsa`, `.pem`, `.key`, `credentials.json`, two service-account JSON names). It blocked 20 fake key lines: Stripe `sk_test_`/`sk_live_`/`rk_`/`whsec_`, OpenAI, Anthropic, AWS, two GitHub token types, Google, Slack, a private key block, Supabase `sb_secret_` and two `service_role` forms, a Postgres URL with a password, hard-coded `apiKey`/`password` literals, an env line in docker-compose, and a key inside a file named "Placeholder". Each was reported as `file:line` with HEAD unchanged, and no key value was printed in any run. It allowed 12 clean cases: normal code, `.env.example` with placeholders, `.env.local.example`, `process.env` and `import.meta.env`, `max_tokens`, a Firebase key marked `gitsafety:allow`, long strings, a binary file, a deletion, and a commit from a subfolder.
- **gitleaks, 4 checks** (v8.30.1 on PATH): it caught a fake SendGrid key that the built-in patterns miss, without printing it. `GITSAFETY_NO_GITLEAKS=1` skips it.
- **checkpoint.sh, 28 checks.** Covered: the first commit in an empty repo, new/changed/deleted files, names with spaces and accents, "nothing new" (no empty commit), and a run from a subfolder. Untracked `.env`/`.env.local` files were skipped and left on disk. A fake key was blocked both with and without the hook installed, and each time **the staging area was restored exactly**, including a partial staging the user had made. It refused cleanly with `node_modules` not ignored, on a detached HEAD, mid-merge, and with no git identity. An already-tracked `.env` was not re-saved, with a warning.
- **undo-last.sh, 60 checks.** For uncommitted work: a preview changed nothing (a fingerprint of every file, the index and all refs was identical). `--yes` restored the checkpoint exactly, covering modified, deleted, new, staged, renamed, partly-staged and nested-new files. **Every undone file was byte-identical in the backup branch**, and the documented "one file back" and "everything back" commands restored the exact pre-undo files. `.env` files and `safety-net/` were untouched and not copied. For commits: revert, redo, `--to` several checkpoints back, and backup branches. It refused (with state identical) on the first commit, a merge, a detached HEAD, no commits, and `--to` with uncommitted work. It also refused an undo that would **re-add a removed secret** (`git revert` doesn't run hooks; observed here), with and without the hook. The default **preview** now flags that same case (same hook, no key values printed, fingerprint unchanged) before `--yes` refuses it.
- **Playbook commands, 41 checks**, run exactly as written: restore a deleted file (uncommitted, staged, committed, a whole folder), one file from an old commit, `git stash` without the scripts, `git revert -m 1` on a merge, reflog rescue after `reset --hard HEAD~3` (including a repo whose branch is `master`, using `git branch --show-current` as the playbook now writes), rescue after a bad rebase (`ORIG_HEAD` and the entry below `rebase (start)`), a deleted branch, `git fsck --lost-found` for staged-only work (and proof that unstaged edits really are lost), `git rm --cached .env`, `git filter-repo --sensitive-data-removal` on a fresh clone (2.47.0), GitHub's `git commit --amend --all` fix, the setup hook check, and the checklist's pre-push grep.
- **Install and .gitignore, 58 checks.** Install was idempotent, never overwrote a foreign hook or husky's `core.hooksPath`, and turned a CRLF hook into LF. A `core.autocrlf=true` clone kept the scripts LF and runnable. 25 paths were confirmed ignored and 14 confirmed not ignored (`.env.example`, `src/out/`, Supabase migrations and `config.toml`, and others).
- **PowerShell, 33 checks.** Run with PowerShell 7.7 on Linux: install, checkpoint (blocked key, staging restored, key not printed), undo-last preview / `-Yes` / revert / `-To`, refusing the first commit and a secret re-add (the preview now warns too, without printing the key), and leaving `safety-net/` alone. PSScriptAnalyzer found 0 issues in all three scripts, and 0 syntax problems for Windows PowerShell 5.1.
- **shellcheck 0.10.0:** 0 findings on the three scripts and the POSIX-sh hook. **Kit files:** no `test-*` names, the JSON is valid, and gitleaks finds nothing in the kit.

**Not tested:** real GitHub (push protection blocking a push, secret scanning alerts, the bypass flow), real Vercel (Instant Rollback, Manage/promote, `vercel rollback`), and the in-app checkpoints of Cursor, Claude Code, Lovable, Replit and Bolt. Those are documented from their current docs only (see `sources.md`), and their menu names may change. Also not tested: the `.claude/settings.json` ask/deny rules and `.cursor/permissions.json` inside the real tools (JSON validated only), Windows itself (Git Bash, Windows PowerShell 5.1 runtime, Git for Windows' hook runner), macOS, and Git older than 2.47.

## Sources
See sources.md (in the kit .zip) (Vercel, GitHub, Git, Cursor, Claude Code, Lovable, Replit, Bolt, gitleaks and template docs, checked 2026-11-23).

---
This kit is free from Bot Pantry: https://botpantry.com/kits/git-safety-net
Share that link with anyone it would help. New kits added regularly, plus a weekly roundup of the most-downloaded ones: https://botpantry.com/newsletter
