vlogstack
Ask

How to undo the last Git commit

SolvedGitCodeman · ep. 0116.8M views on Stack Overflow ↗updated Nov 23, 2026git-resetgit-reset-softgit-reset-hardundo-last-commit
Video cover: Undo your last Git commit (keep or drop the changes)
VideoThe direct link to this video is added here when it is public.

The problem

You made a commit you did not want: a wrong file, a wrong change or a wrong message. You want the commit gone, and you want to choose whether your changes stay or go.

Asked on Stack Overflow: How do I undo the most recent local commits in Git? (16,829,196 views as of Nov 23, 2026)

Solution

commands.sh
git log --oneline
# undo the commit, keep the changes
git reset --soft HEAD~1
git log --oneline
git status --short
# or throw the changes away too
git reset --hard HEAD
git status --short

Soft keeps your work, hard deletes it.

Step by step

  1. Look at the history. git log --oneline shows three commits, and the last one is the mistake.
  2. Remove the commit, keep the work. git reset --soft HEAD~1 removes the commit but keeps your changes.
  3. Check that nothing is lost. git status --short shows the changes are safe and staged (M app.txt).
  4. Optional: throw the changes away too. git reset --hard HEAD discards them. Be careful: this cannot be undone.
What it looks like in a real run
$ git log --oneline
a58394d oops
28065b8 add header
5900691 add app
# undo the commit, keep the changes
$ git reset --soft HEAD~1
$ git log --oneline
28065b8 add header
5900691 add app
$ git status --short
M  app.txt
# or throw the changes away too
$ git reset --hard HEAD
HEAD is now at 28065b8 add header
$ git status --short

Heads-up

If the commit is already pushed, do not reset: use git revert HEAD instead, because reset rewrites history that your teammates may already have.