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

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.shCopied
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
- Look at the history.
git log --onelineshows three commits, and the last one is the mistake. - Remove the commit, keep the work.
git reset --soft HEAD~1removes the commit but keeps your changes. - Check that nothing is lost.
git status --shortshows the changes are safe and staged (M app.txt). - Optional: throw the changes away too.
git reset --hard HEADdiscards 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.