git revert creates a new commit that undoes the changes of a previous commit — safe for shared branches. git reset moves the branch pointer backward, optionally modifying the staging area and working directory — use with caution on shared branches.
Git
Beginner
11 min read
git revert and git reset
Example
# ---- git revert (safe, keeps history) ----
# Creates a NEW commit that undoes the specified commit:
git revert abc1234
# Revert without opening an editor (use default message):
git revert --no-edit abc1234
# Revert multiple commits:
git revert abc1234 def5678
# ---- git reset (rewrites history — avoid on shared branches) ----
# --soft: move HEAD back, keep changes STAGED:
git reset --soft HEAD~1
# Use case: undo last commit but keep everything staged for a new commit
# --mixed (default): move HEAD back, keep changes in working dir:
git reset HEAD~1
git reset --mixed HEAD~1
# Use case: undo last commit, unstage changes (keep files)
# --hard: move HEAD back, DISCARD all changes:
git reset --hard HEAD~1
# WARNING: This permanently deletes uncommitted changes!
# Reset to a specific commit:
git reset --hard abc1234
# Recover from an accidental hard reset using reflog:
git reflog
# HEAD@{1}: commit: The commit I just lost
git reset --hard HEAD@{1}