Undo changes safely with revert, or rewrite history with reset --soft/--mixed/--hard.
Undoing Changes
# Amend last commit (before push)
git commit --amend -m "corrected message"
git commit --amend --no-edit # keep message, add staged files
# Revert a commit (safe — creates new commit)
git revert abc1234
git revert HEAD~2 # revert 2 commits ago
git revert --no-commit HEAD~3..HEAD # revert range
# Reset (dangerous — rewrites history)
git reset --soft HEAD~1 # undo commit, keep changes staged
git reset --mixed HEAD~1 # undo commit, unstage changes (default)
git reset --hard HEAD~1 # undo commit, DISCARD changes
# Recover lost commits with reflog
git reflog
git checkout abc1234