Lesson Completion
Back to course

git revert

Beginner
9 minutes4.8GitPlay to LearnLearn by Story

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:

Aspectgit resetgit revert
What it doesMoves branch pointer backwardCreates a new "undo" commit
HistoryRewritten (commits removed)Preserved (only grows)
Safe for shared branches?❌ No✅ Yes
Requires force-push?Yes (if pushed)No
Reversible?Via reflogJust 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

gitGraph commit id: "A" commit id: "B (bug)" commit id: "C" commit id: "Revert B"

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

bash
# ─── 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 --abort

Key Takeaways

  • git revert creates a new commit that undoes a previous commit — history is preserved.
  • It's the only safe way to undo on shared branches.
  • Use -m 1 when reverting merge commits.
  • Revert conflicts are possible and resolved the same way as merge conflicts.

Interview Prep

  • Q: When should you use git revert instead of git reset? A: Use revert when the commit has been pushed to a shared branch. revert adds a new undo commit without erasing history, so it doesn't break anyone else's clone. reset should 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 1 flag specifies which parent to keep (usually 1 for 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 add the resolved files, and run git revert --continue.

Topics Covered

Git HistoryGit Fundamentals

Tags

#git#revert#undo#beginner-friendly

Last Updated

2026-02-13