Skip to content

How to undo the last Git commit (and which way to choose)

Undo the last Git commit with reset --soft, reset --hard, revert or commit --amend, choose by whether you pushed, and get a lost commit back with reflog.

Checked 2026-11-19

The right command depends on one question: has the commit been pushed to a branch other people use?

Not pushed yet

Fix the message or add a forgotten file:

git add forgotten.txt
git commit --amend            # opens the editor; or -m "better message"

Undo the commit but keep the changes staged:

git reset --soft HEAD~1

Undo the commit and unstage the changes (they stay in your files):

git reset HEAD~1              # same as --mixed

Throw the commit and its changes away:

git reset --hard HEAD~1

--hard also discards any uncommitted work in your files. Run git status first.

Already pushed

Do not rewrite shared history: anyone who pulled the old commit will get conflicts. Add a new commit that reverses it:

git revert <commit>           # HEAD for the last one
git push

revert is safe on main. For a merge commit, add -m 1 to say which parent to keep.

Got it wrong? reflog

Git keeps a log of where HEAD has been, including commits no branch points at any more:

git reflog
# a1b2c3d HEAD@{1}: commit: the commit you just reset away
git reset --hard a1b2c3d      # or: git branch rescue a1b2c3d

Reflog entries last about 90 days by default, and they are local: they are not on the server and not in someone else's clone.

Practise it on a real machineHistory Surgery: Undo, Recover & Cherry-pick lab
Open the lab