What is the difference between git reset and git revert?
- Technology:
- Git
Quick answer
git revert creates a new commit that undoes an earlier one and is safe on shared branches; git reset moves the branch pointer back to an earlier commit, effectively removing later commits from that branch鈥檚 history.
Detailed explanation
git revert <commit> calculates the inverse of a commit and records it as a new commit. History only grows, so teammates who already pulled are not affected. This is how you undo a bad change that has reached main or production.
git reset moves the current branch to another commit. With --soft the undone changes stay staged, with the default --mixed they stay in your working tree unstaged, and with --hard they are discarded. It is ideal for cleaning up local commits that have not been pushed.
If you reset by mistake, the commits are usually still recoverable for a while through git reflog, which records where your branch pointed before each move.
Example
# undo a pushed commit safely
git revert a1b2c3d
# rewrite local history (not yet pushed)
git reset --soft HEAD~1 # undo commit, keep changes staged
git reset --hard HEAD~1 # undo commit and discard changes
# recover from a bad reset
git reflog
git reset --hard HEAD@{2}Key points
- revert: new commit, history preserved, safe when shared
- reset: moves the branch pointer, rewrites history
- --soft keeps changes staged, --mixed unstaged, --hard discards them
- git reflog can recover "lost" commits
Common mistakes
- Using git reset --hard on a branch others share, then force-pushing.
- Forgetting that --hard also discards uncommitted work in the working tree.
Follow-up questions
- What is git reflog?
- What is the difference between git merge and git rebase?