Undo Last Git Commit (Soft and Hard)
Git commands to undo the last commit while keeping changes staged, unstaged, or fully discarded.
# 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.
Git Revert Commit Safely
Create a new commit that undoes changes from a previous commit without rewriting history.
Git Reset and Restore File Changes
Commands to discard, unstage, or restore file changes using git restore and git checkout.
Git Rebase Workflow Example
Step-by-step git rebase workflow to keep feature branches up to date with main branch cleanly.
Git Cherry-Pick Commits Example
Apply specific commits from one branch to another using cherry-pick with conflict resolution.