Skip to content
mentoor

Topic 2 of 4 · 5 min read

Checkpoints, rewind and undo for agents

Goal: Roll back an agent's bad change with checkpoints or git and name the side effects a rewind cannot undo.

Use the agent's checkpoint rewind for quick undo of its own file edits, but treat git, committed before each risky step, as the real safety net, because a rewind cannot undo what the agent did outside its edit tool. Know exactly which of the two you are relying on.

Check yourself · 1 of 3

An agent edited eight files and also ran a database migration. You use the checkpoint rewind. What is true afterwards?

Choose one answer

Why it matters

Agents make large changes quickly. Without an undo, a bad run means hunting through a dozen files by hand. Tools like Claude Code snapshot files before each of the agent's edits, and editor agents such as the ones in VS Code let you keep or discard each edit. These cover the common case well. They do not cover everything the agent can touch.

How to do it

  1. Commit before you hand over a risky task. A clean working tree gives you a known state. The cost is one commit; the benefit is a one-line return path.
  2. Rewind for edits. In Claude Code, the rewind command (also reached by pressing Esc twice on an empty prompt) lets you restore the code, the conversation or both to the point before a prompt. Use it when the agent's edits went wrong but the rest of your machine is fine.
  3. Rewind the conversation, not only the code. If a wrong idea is in the context, restoring only files leaves the idea behind. Restore the conversation too, so the agent forgets the dead end.
  4. Use git for everything else. Compare with git diff, discard with git restore on chosen paths, or return to the commit. If you committed something bad, git reflog shows earlier positions of the branch.
  5. List what a rewind cannot undo. The Claude Code docs state that changes made by shell commands, edits by subagents, edits you make outside the session and linked files are not reliably restored, and that checkpoints are not a replacement for version control.

Before and after

Weak: the agent runs a migration and a cleanup script, then edits ten files. You rewind. The files return, but the migration already changed the development database and the script deleted a cache folder. The code now disagrees with the data.

Better: before the task you commit, and you tell the agent to run migrations only against a throwaway database. After the bad run:

git status --short
git restore src/ tests/
git clean -n   # preview untracked files first

For the database you drop the throwaway copy and recreate it from the seed.

What goes wrong

  • Trusting rewind for side effects. Database writes, installed packages, deleted files, sent emails and calls to external APIs stay done.
  • Editing alongside the agent. Your own edits during a session can be overwritten by a restore.
  • Using git reset --hard blindly. It also discards your own uncommitted work. Preview with git status first.
  • No commit, no net. Without a commit, a restore of the wrong point leaves nothing to return to.

Examples

What a rewind restores and what it leaves

The agent did Rewind restores it? Real net
Edited 6 source files Yes git restore
Ran a database migration No Recreate the throwaway database
Ran npm install of a new package No git restore package files, reinstall
Deleted a folder with a shell command No git restore if tracked
Sent a test email through an API No Cannot be undone; use a sandbox key

A safe start for a risky task

git switch -c agent/rename-orders
git commit --allow-empty -m "checkpoint before agent run"
# run the agent here, then review
git diff --stat main

The branch isolates the work. If the result is bad you delete the branch; if it is good you review the diff before merging.

Key points

  • Checkpoint rewind restores the agent's own file edits and optionally the conversation; it is a convenience, not a backup.
  • Commit before a risky task so git can return you to a known state in one command.
  • Shell command effects, databases, installed packages and external calls are not undone by a rewind.
  • Rewind the conversation as well as the code when a wrong idea is in the context.
  • Preview with git status and git clean -n before any command that discards files.

Common mistakes

  • Assuming a rewind returns the whole machine to an earlier state, when shell, database and network side effects stay.
  • Starting a risky task with uncommitted work, so a hard reset or restore also destroys your own changes.
  • Restoring files but not the conversation, so the agent keeps reasoning from the rejected approach.
  • Running a destructive git command such as reset --hard or clean -fd without previewing what it will remove.

Check yourself · 2 of 3

You are about to give an agent a risky refactor. What is the best preparation?

Choose one answer

Check yourself · 3 of 3

The agent's code changes are bad, but you also have uncommitted work of your own in other files. Which first step is safest?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic