Claude Sonnet 5.5
Revert vs. Reset on a Shared Branch
Comparison
git revert | git reset | |
|---|---|---|
| What it does | Adds a new commit that inverses the bad commit's changes | Moves the branch pointer backward, discarding commits from history |
| History | Preserved; the bad commit and its undo both appear | Rewritten; the commit disappears |
| Pushing | Normal git push | Requires git push --force, since your local history diverges from the remote |
| Effect on teammates | None. They just pull the new commit | Disruptive. Anyone who pulled the bad commit has history that conflicts with the remote, and they can accidentally re-push it |
| Risk | Very low | High on shared branches (lost work, duplicate commits, messy merges) |
Use git revert for a shared branch. Reset is fine for commits that exist only on your machine, but once others may have pulled the commit, rewriting history causes problems for them.
Commands for the Safe Option (Revert)
Make sure you're on the right branch and up to date:
git checkout your-branch git pullFind the commit hash:
git log --onelineRevert the commit:
git revert <commit-hash>This opens your editor with a default message like
Revert "original message". Save and close to finish.To skip the editor:
git revert --no-edit <commit-hash>Push normally:
git push
Special Cases
Reverting multiple commits (oldest to newest, inclusive):
git revert --no-edit <oldest-hash>^..<newest-hash>Reverting a merge commit (you must specify which parent to keep, usually
1for the main branch):git revert -m 1 <merge-commit-hash>Conflicts during revert: Resolve the files, then:
git add <files> git revert --continueOr back out with
git revert --abort.
One Caveat
If the commit contained a secret (password, API key), reverting does not remove it from history. In that case, rotate the credential immediately, and consider a history rewrite only with your team's coordination.