How to undo AI code changes: checkpoints, rewind and Git
Three layers of undo for AI edits, from one file to a full Git restore, with the exact commands.

Key takeaways
- Commit or branch before an agent starts, so there is a clean point to go back to.
- Use git restore for one file and /rewind in Claude Code for the last few replies.
- Checkpoints miss shell-command edits and most subagent edits; Git does not.
- Use git revert for pushed commits and git reflog to recover from a reset you regret.
To undo changes an AI coding agent made, use the smallest tool that fits: reject or revert the one file that went wrong, rewind the session to a checkpoint before the bad reply, or go back to a Git commit. Checkpoints are quick but only last for the session and miss some kinds of change. Git is the safety net that always works, as long as you committed before the agent started.
Below are the exact steps for each layer, the Claude Code commands, the Git commands that recover almost anything, and a habit that makes undo painless next time.
Before you start: make undo cheap
Undo is easiest when there is a clean point to go back to. Before you hand a task to an agent:
- Commit or stash whatever you were doing, so the agent's work is the only uncommitted change.
- For anything bigger than a small fix, work on a branch:
git switch -c ai/pricing-page. - Ask the agent to commit after each working step, with a clear message. Small commits mean small undos.
If your project is not in Git yet, run git init and make a first commit now. It takes a minute and turns every later mistake into a two-command fix.
Undo one file
Often only one file went wrong while the rest of the change is fine. Throw away the uncommitted changes to that file:
git restore src/pricing.css
On older Git versions the same thing is git checkout -- src/pricing.css. If the agent created a new file you do not want, delete it; Git has nothing to restore for a file it never saw.
Many agent tools also let you accept or reject changes per file from their own diff view. That is the same operation with a button, and it does not need Git.
Rewind to a checkpoint in Claude Code
Claude Code saves a checkpoint every time you send a prompt. To go back, run /rewind (or its alias /undo), or press Esc twice with an empty prompt. Pick the message you want to return to, then choose one of the options from Anthropic's checkpointing docs:
- Restore code and conversation: both go back to that point.
- Restore code: files go back, the chat stays, so the agent still remembers what it tried.
- Restore conversation: the chat goes back, your files stay as they are now.
Restoring only the code is often the best choice: the agent keeps the lesson of the failed attempt and can try a different approach.
What checkpoints do not cover
Checkpoints are a convenience, not a backup. According to the docs:
- Files changed only by shell commands (a formatter, a code generator,
rm) are not tracked. - Edits made by most subagents are not restored, because they happen outside your session's checkpoints.
- Restoring a file can wipe edits you or another session made to it after the checkpoint.
- Snapshots are cleaned up about 30 days after the session last saved one, by default.
Anthropic's own advice is to keep using Git for real version history. Other agents, including Cursor's agent and the Codex app, have their own restore features; check their docs for exactly what each one tracks before relying on it.
Undo everything since the last commit
When the whole attempt is a write-off and you committed before starting, reset the working folder to that commit. First look at what would be removed:
git status
git clean -n
git clean -n is a dry run that lists new, untracked files. If the list is right, discard everything:
git restore .
git clean -fd
Be careful: git clean -fd deletes untracked files and folders for good, including any you created yourself and never committed. That is why the dry run comes first.

Undo a change that is already committed
If the bad change is already in a commit, you have two options:
| Command | What it does | Use it when |
|---|---|---|
git revert <commit> | Adds a new commit that reverses the old one | The commit is pushed or shared with others |
git reset --hard <commit> | Moves the branch back and throws away later commits and uncommitted work | The commits are only on your machine and you want them gone |
Find the commit id with git log --oneline. When in doubt, use revert: it never rewrites history, so it is safe on shared branches.
Recover from an undo you regret
Even after a hard reset, Git usually still has the old commits for a while. git reflog lists every position your branch has been at, newest first. Find the entry from before the reset, note its id, and reset to it:
git reflog
git reset --hard <id-from-reflog>
This only works for work that was committed at some point. Uncommitted changes that were discarded are gone, which is one more reason to commit small and often.
Which layer should you use?
- One file looks wrong: reject or restore that file.
- The last reply or two went sideways: rewind to the checkpoint before them, code only.
- The whole attempt failed: restore to your last commit, or delete the branch.
- A bad commit is already pushed:
git revert.
Before you undo, it is worth a short look at what actually changed. Our routine for reviewing AI-generated code helps you tell a broken change from one that only needs a small fix.
Undo in Ninja Builder
Ninja Builder covers all three layers without the terminal. Each reply that changed files ends with a review card where you can undo one file or all of them; it warns you first when an undo could do harm, for example when the change is already committed or the file was edited again later. Before every reply the app also saves your project's files, so Restore to here on any of your messages lists what will be removed, brought back and changed back, and a Redo button reverses it. The built-in Git client shows the history graph, branches, stashes and commits, so the permanent layer is one click away too (see the Git and checkpoints page and Git panel docs).

Download Ninja Builder for Mac, Windows or Linux and try it free for 14 days; after that: it is $4.99 a month (pricing).
Frequently asked questions
How do I undo changes in Claude Code?
Run /rewind (or /undo), or press Esc twice with an empty prompt, pick the message to return to, and choose to restore code, conversation or both.
Does Claude Code's rewind replace Git?
No. Anthropic describes checkpoints as session-level recovery. They do not track files changed only by shell commands or most subagent edits, so keep committing with Git.
How do I undo all uncommitted changes in Git?
Run git restore . to discard edits to tracked files, then git clean -n to preview new files and git clean -fd to delete them. The clean step is permanent.
What is the difference between git revert and git reset?
git revert adds a new commit that reverses an old one and is safe on shared branches. git reset --hard moves your branch back and discards later commits and uncommitted work.
Keep reading

How to review AI-generated code: a practical routine
Six steps, in the same order every time, to catch what AI coding agents get wrong before it reaches your main branch.

AI agent teams and subagents, explained
What subagents and agent teams are, when splitting the work pays off, and what it really costs.

How to use Claude Code without the terminal
The desktop app, the VS Code extension or a chat app: how to use Claude Code with buttons instead of commands.