git revert for AI agents: undo a pushed commit without rewriting history
TL;DR: git revert adds a new commit that undoes an old one. The old commit stays in the history, the branch keeps moving forward, and everybody who already pulled keeps a clean, linear log. That is the only safe way to take back a commit that is already on main — git reset rewrites history and breaks every clone that has it. Every output below was produced by Git 2.47.3 on Nov 25 2026 in throwaway repositories, not reconstructed from memory.
| The situation | The command | What actually happens |
|---|---|---|
The agent pushed a bad commit to main | git revert <sha> | New commit, new SHA, bad code gone from the tree |
The bad commit is already merged into main | git revert <sha> | Same, and it is a normal fast-forward-able commit |
| The revert hits a conflict | git revert --abort | Branch restored exactly as it was |
| You fixed the conflict by hand | git add <file> then git revert --continue | Creates the revert commit with your resolution |
| The bad commit was a merge | git revert -m 1 <sha> | Required: without -m Git refuses with exit 128 |
| You want to undo three commits as one change | git revert --no-commit <sha1> <sha2> <sha3> | Stages the reversal, creates no commit |
| The commit is already reverted | git revert <sha> | ”nothing to commit, working tree clean”, exit 1, no commit |
Why an agent needs this command
The failure mode is not “the agent wrote bad code”. It is “the agent wrote bad code and it is already on the shared branch”. The agent pushed a commit that broke the build, deleted a file that was supposed to stay, or committed a secret-looking token. The branch is shared. Somebody may already have pulled it.
Now there are two possible moves and only one of them is safe:
git reset --hardrewrites the branch pointer. Everyone else’s clone now disagrees with the remote, and the nextgit pullproduces a merge of two unrelated histories. On a shared branch this is an outage.git revertrecords the undo as a new commit. The bad commit is still in the log (auditable), the tree goes back to the good state, and nobody has to re-clone.
Our git cherry-pick guide covers the opposite problem — moving a good commit somewhere it is missing. git stash covers parking uncommitted work. git worktree covers running two tasks in two directories. Revert covers the remaining case: the damage is committed, and possibly already pushed.
What a revert actually produces
A revert is a patch. It computes the inverse diff of one commit and applies it as a new commit. The new commit gets a new SHA because its parent is different:
$ git revert --no-edit HEAD
[master 2eca0eb] Revert "feat: change a"
Date: Fri Nov 20 10:00:00 2026 +0800
1 file changed, 1 deletion(-)
exit=0
$ git log --oneline -2
2eca0eb Revert "feat: change a"
7d4bfa9 feat: change a
$ git log -1 --pretty=%B
Revert "feat: change a"
This reverts commit 7d4bfa9e236bb29fcfda004b1f04d98f856b502a.
Two things to notice. The message body records the exact SHA that was undone, which is the audit trail — you can always find out which original commit a given revert cancels. And the original commit is still there:
$ git merge-base --is-ancestor 7d4bfa9e236bb29fcfda004b1f04d98f856b502a HEAD && echo "still an ancestor"
still an ancestor
That is the whole point. Nothing was rewritten.
Reverting a revert
Reverting a revert is a normal, supported operation, and Git changes the wording so the log stays readable:
$ git revert --no-edit HEAD
[master 10d65d3] Reapply "feat: change a"
Date: Fri Nov 20 10:00:00 2026 +0800
1 file changed, 1 insertion(+)
exit=0
$ git log -1 --pretty=%B
Reapply "feat: change a"
This reverts commit 2eca0eb97aa360904e6145d23998ad155c098f1b.
Note that the body still says “This reverts commit …” — it points at the revert commit, not at the original. If an agent needs to trace the chain, the message body is the reliable link, not the subject line.
The conflict path, exactly as Git prints it
When the same lines changed on both sides, Git stops with exit code 1, writes the conflict into the file, and records the SHA it was working on:
$ git revert --no-edit HEAD~1
Auto-merging c.txt
CONFLICT (content): Merge conflict in c.txt
error: could not revert de01f71... feat: feature c
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git revert --continue".
hint: You can instead skip this commit with "git revert --skip".
hint: To abort and get back to the state before "git revert",
hint: run "git revert --abort".
hint: Disable this message with "git config advice.mergeConflict false"
exit=1
$ git status --short
UU c.txt
$ cat .git/REVERT_HEAD
de01f71edcd193b1962605563f0715e15d1fafbd
$ cat c.txt
<<<<<<< HEAD
main version
=======
base
>>>>>>> parent of de01f71 (feat: feature c)
Three files or signals an agent should read: the exit code, the UU line in git status --short, and the contents of .git/REVERT_HEAD. The last one is the machine-readable state — it is what git revert --continue and --abort use.
Aborting is safe and complete
git revert --abort returns the branch to exactly the state it was in before the attempt. It is the right move when the conflict is in a file the agent cannot resolve correctly — a lockfile, a generated schema, a merge of two vendored trees:
$ git revert --abort
exit=0
$ git status --short
$ cat c.txt
main version
An empty git status --short is the confirmation that the abort worked.
Resolving and continuing
After you fix the file by hand, stage it and continue the sequence:
$ git revert --no-edit HEAD~1
Auto-merging c.txt
CONFLICT (content): Merge conflict in c.txt
error: could not revert de01f71... feat: feature c
exit=1
$ printf "resolved by hand\n" > c.txt
$ git add c.txt
$ git revert --continue
[master 465f3f5] Revert "feat: feature c"
1 file changed, 1 insertion(+), 1 deletion(-)
exit=0
$ git log -1 --pretty=%B
Revert "feat: feature c"
This reverts commit de01f71edcd193b1962605563f0715e15d1fafbd.
Note that the conflict marker in the file is the revert side versus the current HEAD side, and the third marker names the parent of the reverted commit. That is not identical to a merge conflict you will have seen before, and it is worth reading the file rather than guessing which side to keep.
Git 2.47.3 exposes the whole family of sequence controls:
$ git revert -h
usage: git revert [--[no-]edit] [-n] [-m <parent-number>] [-s] [-S[<keyid>]] <commit>...
or: git revert (--continue | --skip | --abort | --quit)
--quit end revert or cherry-pick sequence
--continue resume revert or cherry-pick sequence
--abort cancel revert or cherry-pick sequence
--skip skip current commit and continue
-n, --no-commit don't automatically commit
-m, --no-]mainline <parent-number>
select mainline parent
-X, --no-]strategy-option <option>
--quit is the one people forget: it clears the sequence state without touching the working tree, which is what you want when you want to stop the sequence but keep the staged result.
Merge commits need -m
A merge commit has more than one parent, so “the inverse diff” is ambiguous. Git refuses to guess:
$ git revert --no-edit HEAD
error: commit 1a8f070c2c92789c5833324f20c77abf616e404f is a merge but no -m option was given.
fatal: revert failed
exit=128
-m 1 means “treat the first parent as the mainline”, which is the usual case for a merge that brought a feature branch into main. The resulting message says so explicitly:
$ git revert --no-edit -m 1 HEAD
[master 0746faa] Revert "merge side into main"
1 file changed, 1 deletion(-)
delete mode 100644 g.txt
exit=0
$ git log -1 --pretty=%B
Revert "merge side into main"
This reverts commit 1a8f070c2c92789c5833324f20c77abf616e404f, reversing
changes made to fbc006f4b4d926b4cfdceefb5c75f904962f2d43.
For an agent, the rule is simple: if the SHA is a merge, -m 1 is the right guess on a feature branch, and -m 2 is the right guess on main when the merge came from main. Getting it wrong does not corrupt anything — it produces a revert that undoes the wrong side, which you then revert again.
Reverting several commits at once
git revert accepts a range and reverts each commit in it, oldest first. If one of them conflicts, the sequence stops at that commit and everything before it stays applied:
$ git revert --no-edit HEAD~2..HEAD~1
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
error: could not revert 2cc1a92... c2
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git revert --continue".
exit=1
That is the reason the range form is dangerous for an unattended agent: it can leave the branch half-reverted. git revert --abort still cleans up, and git status tells you exactly where the sequence stopped.
If you want the reversal staged but not committed — for example so a human can review the net diff — use --no-commit:
$ git revert --no-commit HEAD
exit=0
$ git status --short
M a.txt
$ git log --oneline -2
ecb0b43 feat: change a
61425e6 feat: add a
$ git diff --cached --stat
a.txt | 1 -
1 file changed, 1 deletion(-)
HEAD did not move, the change is staged, and you get one clean commit to review and squash later. This is the shape most agent pipelines want: revert the three bad commits with --no-commit, then make a single “revert bad batch” commit.
Two failure modes worth knowing before you automate this
A dirty working tree stops the revert before it starts:
$ git revert --no-edit HEAD
error: your local changes would be overwritten by revert.
hint: commit your changes or stash them to proceed.
fatal: revert failed
exit=128
The fix is the one Git suggests: stash the local work first, or commit it. Note the exit code is 128, not 1 — if your agent only checks for “non-zero”, it will treat this as a normal conflict and try to continue a sequence that never started.
Reverting something that changes nothing produces no commit at all:
$ git revert --no-edit HEAD~1
On branch master
nothing to commit, working tree clean
exit=1
This happens when you revert a commit twice, or when you revert a commit that only exists on another branch and does not touch the files in your tree:
$ git revert --no-edit b357c643ba9a0ad0571e1980d88de7e38398208f
On branch master
nothing to commit, working tree clean
exit=1
$ ls b.txt
ls: cannot access 'b.txt': No such file or directory
The second case is a classic agent mistake: it grabbed a SHA from git log --all and tried to revert it on a branch where that change never landed. Check git branch --contains <sha> before reverting.
The agent playbook
| Situation | Move | Why |
|---|---|---|
| Bad commit, already pushed, shared branch | git revert <sha> | History is not rewritten, everyone stays in sync |
| Bad commit, not pushed, only on your branch | git reset --hard <sha> is still fine locally | Nothing else has the commit yet |
| Bad commit was a merge | git revert -m 1 <sha> | Git refuses without a mainline |
| Revert conflicted | git revert --abort, decide, then retry | Abort is complete and lossless |
| Three bad commits, one review wanted | git revert --no-commit <sha1> <sha2> <sha3> | One staged change, one commit |
| You need to recover a branch someone deleted | git reflog | Revert is not a recovery tool |
| The fix belongs on another branch | git cherry-pick | Move the good commit, do not undo it |
Honest take: when revert is the wrong tool
Revert is a forward operation. It adds a commit, and every commit it adds is something the next person has to read. For a branch that already has twenty bad commits, twenty reverts is not a fix, it is a second history. In that case, squash the branch or reset it before it goes public.
Revert is also the wrong tool when the commit is not on the shared branch yet. If the bad commit exists only in your clone, git reset --hard is simpler and leaves no trace. Use revert when the commit is out in the world.
And one more honest caveat: revert undoes a diff, not an intent. If the original commit changed a file and also moved a config value that a later commit depends on, the reverted file may leave the repo in a state nobody designed. That is why the exit code, the git status output, and the built diff matter more than the command’s success message.
Sources
- git-revert documentation (git-scm.com) — the authoritative option list, including
-m,--no-commit,--continue,--abort,--skip,--quit. - git-reset documentation (git-scm.com) — the rewrite alternative, for the comparison table above.
- git-cherry-pick documentation (git-scm.com) — the move-a-commit counterpart.
- git-stash documentation (git-scm.com) — the fix for the dirty-tree failure mode.
- Git 2.47.3 release notes (git-scm.com) — the version every output on this page was produced with.
- git-commit documentation (git-scm.com) — how the
Revert "..."message body is written. - Reproduced locally on Nov 25 2026 with Git 2.47.3 on Linux, in throwaway repositories created for this article.
Last updated: Nov 25 2026. Command behaviour is from Git 2.47.3 on Linux; newer Git versions print the same options, but always check git revert -h on the machine you run it on.
Was this article helpful?
Thanks for the feedback — it helps improve future articles.
Need hands-on help?
I offer 1-on-1 technical consulting for local AI setup, GPU selection, and AI coding tool configuration — same topics covered on this site.
Book a session — $49 / hour →Know which coding tool is worth paying for
Hands-on comparisons of AI coding assistants and what each one costs to run — including the local-model path. Sent only when something changes. Unsubscribe anytime.