Chapter 36: Git Revert

Git revert (how to “undo” a commit that has already been pushed to a shared branch — safely)

This is the safest and most polite way to undo something that is already public (on GitHub, in team repo, deployed somewhere), especially when you cannot rewrite history (because other people already pulled the bad commit).

Many beginners confuse git revert with git reset, git checkout, or git restore — so let’s make it crystal clear once and for all.

1. What does git revert actually do? (very simple explanation)

git revert creates a new commit that does the exact opposite of what an old commit did.

  • It does NOT delete the bad commit from history
  • It adds a new commit that reverses the changes
  • History stays linear and honest — everyone sees what happened and how it was fixed
  • Safe to use on shared branches (main, develop, release branches)
  • Safe to push — no one’s work gets broken

Real-life analogy:

  • Someone added a broken button to the app → users are complaining
  • Instead of going back in time and erasing the button commit (which would confuse everyone who already downloaded it), you add a new commit that removes the broken button
  • The timeline now says: “we added it → oops → we removed it” — clean and transparent

2. git revert vs git reset — the table you must remember

Feature / Question git revert git reset
Creates new commit? Yes — opposite changes No — moves branch pointer
Rewrites history? No — history stays the same Yes — changes commit hashes
Safe to use on shared/pushed branch? Yes — recommended No — dangerous (breaks teammates)
Leaves trace of mistake? Yes — honest history No — hides mistake (looks like never happened)
Can recover easily if wrong? Very easy — revert the revert Harder — needs reflog
Best for Production/main branch fixes Local experiments, cleaning your own branch

Rule of thumb in 2026:

  • If the bad commit is already pushed to main or shared branch → use git revert
  • If the bad commit is only on your laptop (never pushed) → use git reset or git commit –amend

3. Realistic Example – Step by Step (copy-paste ready)

Situation You (or teammate) committed and pushed a bad change to main:

Bash

Now users cannot log in → panic!

Goal: Undo commit abc1234 without rewriting history.

Step 1 – Make sure you’re on the right branch

Bash

Step 2 – Revert the bad commit

Bash

Two things can happen:

A – No conflict (most common for simple changes)

Git opens your editor with auto-generated message:

text

Just save & close (or improve message):

text

Git creates new commit:

Bash

B – Conflict (if same lines changed again since then)

Git says:

text

Fix conflict like normal merge:

  • Open file → remove markers → keep good version
  • git add login.html
  • git revert –continue (or git commit)

Step 3 – Push the revert commit

Bash

→ Now production is fixed → History shows the truth: added → removed → restored

4. Other Very Useful Revert Patterns

Revert multiple commits at once

Bash

Revert merge commit (a bit special)

Bash

Create revert without committing (inspect first)

Bash

5. Quick git revert Cheat Sheet (save this)

What you want to do Command Example Notes / When to use
Undo one specific commit (pushed) git revert abc1234 Most common — safe for main
Undo last commit (local only, alternative) git revert HEAD Same as revert but on latest
Revert range of commits git revert bad1^..bad3 Reverts from after bad1 to bad3
Revert merge commit git revert -m 1 merge-hash -m 1 = keep main parent
Revert but don’t commit yet git revert –no-commit bad-hash Review changes first
Continue after fixing conflict git revert –continue After resolving conflict
Abort revert in progress git revert –abort Give up midway

Got the revert feeling now?

git revert = “create a new commit that undoes a previous commit — safely, publicly, honestly”

It’s the polite, team-friendly way to say “sorry, I made a mistake — here’s the fix”.

Next?

  • Want to practice a full “bad push → revert → push” scenario?
  • How to revert a merge commit properly?
  • Or back to pull requests / contributing / GitHub Flow?

Just tell me — we’ll continue step by step. You’re becoming really strong with Git — proud of you! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *