How to undo the last commit

Updated July 31, 2026

"Undo the commit" is four different operations, and two questions pick between them: did the commit go to the server, and what should happen to its changes. Answer those and the command chooses itself.

  • You only need to fix the message or add a forgotten file, and the commit is still local -> git commit --amend.
  • Drop the commit but keep its changes staged in the index, local -> git reset --soft HEAD~1.
  • Drop the commit together with its changes, local -> git reset --hard HEAD~1. The only choice here that loses work.
  • The commit is already on the server and someone may have pulled it -> git revert <hash>. A new commit undoes the old one and leaves the history alone.

Forgot a file in the last commit? Add it and repeat the commit with the same message:

$ git add images.txt
$ git commit --amend --no-edit
[main 02990aa] chapter about deadlines
 Date: Tue Mar 17 12:04:31 2026 +0300
 2 files changed, 2 insertions(+)
 create mode 100644 chapter.txt
 create mode 100644 images.txt

The commit turned out to be one too many, but you still want its edits: move the branch pointer one step back and keep the changes in the index:

$ git reset --soft HEAD~1
$ git status --short
M  report.txt

Use --hard instead of --soft and both the commit and the on-disk edits are gone; this is the one command here that can lose work for good. And once the commit is pushed, you do not rewrite the history, you add to it with a counter-commit:

$ git revert 0da918c
[main 375d973] Revert "fixed the typos"
 1 file changed, 1 deletion(-)

The rule is short and easy to keep. While the commit lives on your machine only, edit it in place with --amend or drop it with reset, and nobody notices. Once it has gone to the server and reached other people, use revert only: it leaves what they already pulled untouched.

Common mistakes

HEAD~1 means "one commit back", not "the last commit". git reset --hard HEAD (without ~1) does not move the history at all, but it quietly wipes every unsaved edit in your files. When you are unsure what is about to go, look at git status and git diff before, not after.

revert works on an unpushed commit too, but it breeds a needless "did it, undid it" pair in the history. While everything is still local, it is cleaner to drop the commit with reset and save revert for what others have already seen.

Where this is in the book

The three reset modes and the choice between reset, amend and revert are covered in the chapters on changes and on first aid.

Chapter 4. Inspect, undo, ignore Chapter 8. First aid: when something goes wrong