How to Roll Back a Bad AI Coding Agent Change
A checkpoint-branch git workflow for undoing AI coding agent commits that touch dozens of files at once, using git reflog, worktrees, stash, and targeted reverts to recover safely.
You asked an agent to refactor an authentication module. Twenty minutes later it had touched 43 files, renamed two config keys, and quietly deleted a rate limiter nobody asked it to remove. Login now throws a 500, and you have no idea which file is responsible. Here's how to roll back a bad AI coding agent change without losing the work that held up: checkpoint first, diff against it, and review the agent's branch in isolation before it touches main.
Why Agent Commits Break Differently Than Human Commits
A human developer's bad change usually leaves a small, findable trail: one commit, a diff you can read in thirty seconds. An agent session behaves nothing like that, producing a dozen commits, or one enormous one, spanning modules it was never asked to touch. Standard advice like "just use git revert" assumes one well-scoped commit, so this workflow treats the whole session as the unit of work, keeping most of what the agent did while discarding the part that broke.
When an AI Agent Broke My Code Mid-Session
The pattern is familiar if you've run agents against a real codebase. Tests pass after file 12, then the agent edits a shared utility on file 30 and everything downstream silently changes behavior, and eyeballing the resulting diff by hand doesn't scale.
Before You Touch Anything: Diagnose the Damage
Resist the urge to start undoing changes immediately. First figure out the actual scope of what changed and when.
# See recent history, oldest problem is often several commits back
git log --oneline -20
# See every file the agent touched, not just the last commit
git diff --stat HEAD~10
# See a full reflog of everything HEAD has pointed to,
# useful if commits got squashed, amended, or reset already
git reflog show HEADThe reflog is a safety net you don't have to set up in advance. A careless git reset --hard won't lose work: git reflog show HEAD lists the SHA, and git branch recovered-work <sha> gets it back.
How to Roll Back a Bad AI Coding Agent Change: The Checkpoint-Branch Workflow
The fix that holds up for agent-driven, multi-file commits is a checkpoint-branch workflow: give every session a known-good starting point and its own branch, so a bad session leaves a clean diff boundary to inspect.
Step 1: Tag a Checkpoint Before the Agent Starts
Before you hand a codebase to an agent for anything beyond a trivial one-file fix, mark where you are.
# Tag the exact commit you're starting from
git tag checkpoint-$(date +%Y%m%d-%H%M)
# Then give the session its own branch instead of working
# directly on main or your primary feature branch
git switch -c agent/auth-refactor-2026-08-11The tag costs nothing and never moves. Even if you delete the branch or the agent rewrites history, it still points at the last state you know worked. Four seconds, and it's the most important habit here.
Step 2: Diff the Whole Session Against the Checkpoint
Let the agent run. When the session ends, or when something breaks, don't read commit by commit, read the whole session against the tag.
# High-level view: which files changed and by how much
git diff checkpoint-20260811-0930 --stat
# Full diff of everything the agent did this session
git diff checkpoint-20260811-0930
# Diff a single suspicious file against the checkpoint
git diff checkpoint-20260811-0930 -- src/auth/session.py
# List every commit the agent made this session
git log --oneline checkpoint-20260811-0930..HEADDiffing against the tag rather than HEAD~1 matters because agent sessions rarely map cleanly to "one commit ago," no matter how many commits, amends, or rebases happened in between.
Step 3: Isolate the Agent's Branch in a Worktree
Before merging anything, review the agent's branch in its own working directory instead of checking it out over your current one. git worktree checks out two branches at once, in separate folders, sharing one repository.
# Check out the agent's branch into a separate directory
git worktree add ../review-auth-refactor agent/auth-refactor-2026-08-11
# Run the test suite there, independent of your main working tree
cd ../review-auth-refactor && npm test
# Once you're done reviewing, remove the worktree
git worktree remove ../review-auth-refactorThis matters for agent output: you'll want to keep working in your primary directory while the changes are under review. Checking out the branch in place blocks you until you decide; a worktree removes that constraint.
Step 4: Stash, Cherry-Pick, or Reset
Once you know what broke, pick the narrowest tool that fixes it.
If you have uncommitted agent changes you want to set aside temporarily without losing them, run git stash push -m "agent session wip", then git diff checkpoint-tag to compare cleanly, and git stash pop to bring the changes back.
If most of the session was good and only a few commits were bad, stay on the checkpoint branch, use git reset --hard checkpoint-20260811-0930 to wipe it clean, then git cherry-pick <sha> the specific good commits back on, one at a time, in order.
If the entire session was bad and nothing has been pushed anywhere, git reset --hard checkpoint-20260811-0930 on the agent branch throws away everything since the tag in one step.
If any of the agent's commits have already been pushed, merged, or pulled by someone else, do not reset. Use git revert <sha> for each bad commit instead, since revert adds a new commit undoing the change rather than rewriting history other people may already have.
That distinction is what people get wrong under pressure. Reset is fast but destructive to shared history; revert is messier in the log but safe on any shared branch.
How to Undo an AI Coding Agent Commit That's Already Merged
If the bad change made it past review and landed on main, the checkpoint tag no longer covers you. You need to undo the specific commit without touching everything after it.
# Find the exact commit that introduced the problem
git log --oneline --follow -- src/auth/session.py
# Revert it, creating a new commit that undoes the change
git revert a1b2c3d
# Reverting a merge commit needs a parent number, usually 1
git revert -m 1 a1b2c3d
# Reverting a range of consecutive bad commits
git revert --no-commit older_sha^..newer_sha
git commit -m "Revert agent auth session, see incident notes"git bisect is worth knowing too. If you're unsure which of several agent commits introduced the regression, run git bisect start, mark a known-good and known-bad commit, and git binary-searches the range for you.
Git Recovery for AI Coding Agent Sessions: A Quick Checklist
Print this out, or keep it next to whatever terminal you run agent sessions from.
Tag a checkpoint before every agent session that touches more than one or two files.
Put the session on its own branch, never directly on main.
After the session, run git diff checkpoint-tag --stat before reading anything else.
Review the branch in a git worktree, not your primary working directory.
Cherry-pick the good commits, revert or reset the bad ones depending on whether anything has been shared.
If something looks lost, check git reflog show HEAD before assuming it's gone.
None of this requires trusting the agent less. A session touching dozens of files needs a session-sized safety net, not a commit-sized one. The checkpoint tag and isolated branch turn a stressful cleanup into a short, known sequence.
FAQ
How do I undo an AI coding agent commit after it's already been merged to main?
Do not reset a shared branch. Use git revert <commit-sha> instead. Revert creates a new commit that undoes the changes from the target commit while preserving history, which is safe on a branch other people have already pulled. If the agent's work spans several commits, revert them in reverse order (newest first) or pass a range like git revert --no-commit oldest^..newest and commit once you've resolved any conflicts.
What if git reflog doesn't show the commit I need?
Reflog entries expire, by default unreachable entries after 30 days and reachable ones after 90, and reflog is local to your machine, so it won't help if the damage happened in someone else's clone. Run git reflog show HEAD first, then check git reflog show <branch-name> for branch-specific history. If the commit object still exists but nothing points to it, git fsck --lost-found will list dangling commits and blobs you can inspect with git show.
Can I roll back just some of the agent's changes and keep the rest?
Yes. This is the main argument for reviewing an agent's work in a worktree before merging. Check out the agent's branch in its own working directory, look at the diff file by file, and cherry-pick only the commits or hunks you want with git cherry-pick <sha> or git checkout <checkpoint-tag> -- path/to/file.py to restore a single file to its pre-agent state while leaving everything else untouched.
How do I stop an AI coding agent from breaking my code again?
You can't fully prevent it, agents will occasionally make a wrong call on a large refactor, but you can shrink the blast radius. Tag a checkpoint before every agent session, keep agent work on its own branch instead of your main working branch, run the test suite against the checkpoint diff before merging, and avoid letting a single session run unsupervised across more files than you're prepared to review in one sitting.
Is git revert or git reset better for undoing an agent's commit?
It depends on whether the commit has been shared. git reset rewrites history and is fine on a private, unpushed branch, for example resetting your local checkpoint branch back to the tag before a bad agent session. git revert adds a new undo commit instead of rewriting anything, which is the correct choice once the agent's commits have been pushed, merged, or pulled by anyone else.
Related reading
How did this land?
About the author

Developer Advocate
Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.


