What is the difference between git reset and git revert for undoing a commit?
Options
- They are identical in the effect they have on history
resetrewrites history;revertadds an undoing commit (Correct option)revertdeletes the whole repository from the diskresetonly works on remote branches, never local
Answer
reset moves the branch back, rewriting history (risky if already pushed); revert creates a NEW commit that undoes the old one, safe for shared branches
Why
git reset --hard <commit> moves your branch pointer back and discards later commits and changes. Fine for local work, but if others already have those commits, you create a mess (it rewrites history).
git revert <commit> adds a new commit with the opposite changes. History stays intact, so it is the safe way to undo something already pushed to a shared branch like main.
git revert 3f2a9c1 # safe on shared branches
git reset --hard HEAD~1 # local only: drops last commit and changesRemember
Shared branch: revert. Local mess: reset.