bashbeginner
Undo Last Git Commit (Soft and Hard)
Git commands to undo the last commit while keeping changes staged, unstaged, or fully discarded.
bashPress ⌘/Ctrl + Shift + C to copy
# Undo commit, keep changes staged
git reset --soft HEAD~1
# Undo commit, keep changes unstaged
git reset --mixed HEAD~1
# Undo commit AND discard all changes (destructive)
git reset --hard HEAD~1
# Undo commit but create a new revert commit (safe for shared branches)
git revert HEAD
# Undo last N commits (keep changes staged)
git reset --soft HEAD~3
# Check what will be undone before resetting
git log --oneline -5Use Cases
- Fixing a commit with wrong files or message
- Removing accidental commits before pushing
- Safely reverting changes on shared branches
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
bashbeginner
Git Reset — Soft, Mixed, and Hard Explained
Understand the three git reset modes and when to use each for undoing commits safely.
Best for: Undoing accidental commits before pushing
#git#reset
bashbeginner
Git Revert Commit Safely
Create a new commit that undoes changes from a previous commit without rewriting history.
Best for: Undoing a deployed commit on shared branches
#git#revert
bashbeginner
Git Reset and Restore File Changes
Commands to discard, unstage, or restore file changes using git restore and git checkout.
Best for: Discarding experimental changes in files
#git#reset
bashintermediate
Git Reflog Recovery Guide
Use git reflog to recover lost commits, undo hard resets, and restore deleted branches.
Best for: Recovering from accidental git reset --hard
#git#reflog