Blog / git
Undo a coding agent's last commit (and keep the changes)
An agent committed something you didn't want. Undo the commit and keep its changes, drop it entirely, undo a bad amend, or revert it once it's pushed.
On this page
- Not pushed: undo the commit, keep the changes staged
- Not pushed: undo the commit, keep the changes as edits
- Not pushed: throw the commit away
- The agent amended a commit you wanted
- Already pushed: revert it
- Which one to use
- Ask the agent to commit less eagerly
- Where Joxo fits
- Frequently asked questions
- How do I undo the last commit but keep the changes?
- How do I undo a commit that is already pushed?
- How do I undo git commit --amend?
- What is the difference between git reset and git revert?
- Try it with your team
Your agent committed before you looked: the wrong files, a half-done change, a message that says "fix", or a change you didn't ask for. Which command undoes it depends on two things: whether you want to keep the changes, and whether the commit has been pushed. We ran every command with git 2.43 on a scratch repository.
First, stop the agent or let it finish. If it commits again while you undo, you'll be undoing the wrong commit.
Not pushed: undo the commit, keep the changes staged#
git reset --soft HEAD~1
git status --shortM aThe commit is gone and its changes are staged, exactly as they were a moment before the agent ran git commit. Fix what was wrong and commit again with a better message. This is the one to use for "right change, wrong message" or "it forgot a file".
Not pushed: undo the commit, keep the changes as edits#
git reset HEAD~1Unstaged changes after reset:
M aSame as above, but nothing is staged. Use this when the commit mixed things together and you want to stage and commit them in pieces (see split a coding agent's big commit).
Not pushed: throw the commit away#
git reset --hard HEAD~1The commit and its changes are gone from your folder. --hard also discards anything you haven't committed yet, so check git status first. If you change your mind, the commit is still in git reflog for a while (see what you can get back after git reset --hard).
HEAD~1 means one commit back. If the agent made three you don't want, use HEAD~3, and check what you're about to undo with git log --oneline -3 first.
The agent amended a commit you wanted#
Agents like git commit --amend, which replaces the last commit instead of adding one. If it amended your commit with its change, the reflog has your original:
git reflog -375b1cd7 HEAD@{0}: commit (amend): Real amended
b1a6d59 HEAD@{1}: commit: Real
7bb3e58 HEAD@{2}: revert: Revert "Agent: bad"Go back to the commit from before the amend, keeping the agent's addition staged:
git reset --soft HEAD@{1}
git diff --cached --stat q | 1 +
1 file changed, 1 insertion(+)Your commit is back as it was, and the staged diff is just what the agent added. Commit it separately, or drop it with git restore --staged --worktree q (that discards the agent's edit to q).
Already pushed: revert it#
Once a commit is on a shared branch, don't rewrite it: a teammate or their agent may already have pulled it. Make a new commit that undoes it instead:
git revert --no-edit HEAD
git log --oneline -27bb3e58 Revert "Agent: bad"
749aceb Agent: badThen push as normal. The history shows both the change and its undo, which is what you want on main. To revert an older commit, give its hash instead of HEAD. If the commit you want undone is a merge, add -m 1 to keep the side it was merged into.
If the branch is yours alone and nobody else has pulled it, you can reset as above and git push --force-with-lease instead. If the agent has already force-pushed over someone else's work, see a coding agent force-pushed over my work.
Which one to use#
| You want | Not pushed | Pushed |
|---|---|---|
| Keep the changes, redo the commit | git reset --soft HEAD~1 | git revert HEAD, then commit the fix |
| Keep the changes, unstaged | git reset HEAD~1 | same as above |
| Drop the change entirely | git reset --hard HEAD~1 | git revert HEAD |
| Undo an amend | git reset --soft HEAD@{1} | git revert HEAD, then commit the original part again |
Ask the agent to commit less eagerly#
Most unwanted commits come from an agent told to "commit when done" deciding it is done. Add one line to your project instructions: "Don't commit until I've reviewed the diff. Never use git commit --amend on a commit you didn't make." If you'd like the agent to keep committing as it goes, ask for small commits with real messages instead, and squash them before merging.
Where Joxo fits#
With several people's agents on one project, an unwanted commit on a shared branch affects everyone who pulled it. Joxo shows each agent's work on the project as it happens and asks you on your phone before the steps you've marked as needing you. Setup is one line pasted into your agent.
Frequently asked questions#
How do I undo the last commit but keep the changes?#
Run git reset --soft HEAD~1. The commit is removed and its changes stay staged. Use git reset HEAD~1 to keep them as unstaged edits instead.
How do I undo a commit that is already pushed?#
Run git revert HEAD and push. It adds a new commit that undoes the old one, so nobody who pulled the branch has to reset.
How do I undo git commit --amend?#
Run git reset --soft HEAD@{1}. It moves back to the commit from before the amend and leaves what the amend added staged.
What is the difference between git reset and git revert?#
git reset moves the branch back and removes commits from it. git revert keeps every commit and adds a new one that undoes a change, which is safe on a shared branch.