git undo last commit — keep or discard changes
Quick Answer
# Keep changes staged (safest — just undoes the commit)
git reset --soft HEAD~1
# Keep changes but unstage them (default behavior)
git reset HEAD~1
# Discard all changes entirely (destructive — cannot undo)
git reset --hard HEAD~1
When to use this
You just ran git commit and want to undo it — to fix the message, add a forgotten file, or split it into smaller commits.
More options
Undo multiple commits at once
git reset --soft HEAD~3 # undo last 3 commits, keep changes staged
git reset HEAD~3 # undo last 3, keep changes unstaged
Undo a commit that was already pushed
Use git revert instead of git reset — it adds a new "undo" commit without rewriting history.
git revert HEAD # creates a new revert commit
git push origin main
Related