Revert a commit safely
Medium risk
What do risk levels mean?
- Read-only
- Inspects state without changing anything.
- Low risk
- Reversible with a routine follow-up command.
- Medium risk
- Changes state; undo path documented.
- Destructive
- Deletes or overwrites; confirmation required.
Creates a new commit that undoes the changes of an earlier commit, without rewriting history — the safe way to undo on a shared branch.
gitrevertundo
Parameters
Hash (or ref) of the commit to revert.
Commands
Confirm which commit and files the revert will undo
git show --stat <commit>Create a new commit that reverses the selected commit
git revert <commit>Verification
git log --oneline -2A new "Revert ..." commit sits on top of the branch.
Undo
Revert the revert commit, restoring the original changes
git revert HEADPitfalls
- Reverting can conflict when later commits touched the same lines; resolve and run git revert --continue.
- Reverting a merge commit needs -m 1 to pick the mainline parent.