How to undo the last Git commit

· Category: Git

Short answer

Use git reset for local unpushed commits and git revert for public commits you need to undo safely.

Steps

  1. Undo last commit but keep changes:
git reset --soft HEAD~1
  1. Undo last commit and discard changes:
git reset --hard HEAD~1
  1. Create a reversal commit:
git revert HEAD
  1. Amend the last commit:
git commit --amend

Tips

  • --soft is safest for undoing while preserving your work.
  • --hard is destructive; ensure you do not need the changes.
  • revert is the correct choice for shared history.

Common issues

  • Resetting pushed commits breaks history for collaborators.
  • Accidental --hard can be recovered with git reflog in most cases.