The Hook (The "Byte-Sized" Intro)
A developer pushes a bug to main. Reverting with git reset would break everyone's clone. git revert is the civilized solution: it creates a new commit that undoes the bad one, keeping history intact. Everyone pulls the fix. Nobody's repo breaks. No angry Slack messages. This is how professionals undo mistakes on shared branches.
📖 What is git revert?
git revert creates a new commit that contains the exact inverse of a specified commit. The original commit stays in history — nothing is erased. This makes it safe for shared branches where reset would be destructive.
Conceptual Clarity
Reset vs Revert:
| Aspect | git reset | git revert |
|---|---|---|
| What it does | Moves branch pointer backward | Creates a new "undo" commit |
| History | Rewritten (commits removed) | Preserved (only grows) |
| Safe for shared branches? | ❌ No | ✅ Yes |
| Requires force-push? | Yes (if pushed) | No |
| Reversible? | Via reflog | Just revert the revert |
Real-Life Analogy
reset= Tearing a page out of a shared notebook (everyone notices the missing page)revert= Adding a correction note on the next page (everyone can see both the mistake and the fix)
Visual Architecture
Why It Matters
- Safe undo: The only way to undo on shared branches without breaking teammates' repos.
- Auditable: The revert commit explains what was undone and why.
- Reversible: If you need the original change back, you can "revert the revert."
- CI-friendly: Works with branch protection rules — no force-push needed.
Code
# ─── Revert a single commit ───
git revert abc1234
# Opens editor for commit message (auto-generated)
# "Revert 'Add broken feature'"
# ─── Revert without opening editor ───
git revert abc1234 --no-edit
# ─── Revert multiple commits (one by one) ───
git revert abc1234
git revert def5678
# ─── Revert a range of commits ───
git revert abc1234..def5678 --no-edit
# Creates one revert commit per original commit
# ─── Revert a merge commit ───
git revert -m 1 abc1234
# -m 1 = keep the first parent (usually main)
# ─── If revert causes a conflict ───
# Resolve the conflict manually, then:
git add .
git revert --continue
# ─── Abort a revert in progress ───
git revert --abortKey Takeaways
git revertcreates a new commit that undoes a previous commit — history is preserved.- It's the only safe way to undo on shared branches.
- Use
-m 1when reverting merge commits. - Revert conflicts are possible and resolved the same way as merge conflicts.
Interview Prep
-
Q: When should you use
git revertinstead ofgit reset? A: Userevertwhen the commit has been pushed to a shared branch.revertadds a new undo commit without erasing history, so it doesn't break anyone else's clone.resetshould only be used on local, unpushed commits. -
Q: How do you revert a merge commit? A: Use
git revert -m 1 <merge-commit-hash>. The-m 1flag specifies which parent to keep (usually1for the main branch). Without-m, Git doesn't know which side of the merge to undo. -
Q: Can a revert cause conflicts? A: Yes. If the code has changed since the original commit, the inverse changes may conflict with newer modifications. In that case, resolve the conflicts manually,
git addthe resolved files, and rungit revert --continue.