bashbeginner

Undo Last Git Commit (Soft and Hard)

Git commands to undo the last commit while keeping changes staged, unstaged, or fully discarded.

bash
# 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 -5

Use 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.