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:
|
0 1 2 3 4 5 6 7 8 9 10 |
# Bad commit already on GitHub git log --oneline -3 # abc1234 (HEAD -> main, origin/main) Remove login button by mistake # def5678 Add user profile page # 789abcd Initial structure |
Now users cannot log in → panic!
Goal: Undo commit abc1234 without rewriting history.
Step 1 – Make sure you’re on the right branch
|
0 1 2 3 4 5 6 7 |
git switch main git pull origin main # be 100% up-to-date |
Step 2 – Revert the bad commit
|
0 1 2 3 4 5 6 |
git revert abc1234 |
Two things can happen:
A – No conflict (most common for simple changes)
Git opens your editor with auto-generated message:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
Revert "Remove login button by mistake" This reverts commit abc1234. # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. |
Just save & close (or improve message):
|
0 1 2 3 4 5 6 |
fix: restore login button that was accidentally removed |
Git creates new commit:
|
0 1 2 3 4 5 6 7 |
[main def5678] fix: restore login button that was accidentally removed 1 file changed, +5 -5 |
B – Conflict (if same lines changed again since then)
Git says:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
Auto-merging login.html CONFLICT (content): Merge conflict in login.html error: could not revert abc1234... Remove login button by mistake hint: after resolving the conflicts, mark the corrected paths hint: with 'git add <paths>' or 'git rm <paths>' hint: and commit the result with 'git commit' |
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
|
0 1 2 3 4 5 6 |
git push origin main |
→ Now production is fixed → History shows the truth: added → removed → restored
4. Other Very Useful Revert Patterns
Revert multiple commits at once
|
0 1 2 3 4 5 6 |
git revert abc1234^..def5678 # reverts everything from after abc1234 up to def5678 |
Revert merge commit (a bit special)
|
0 1 2 3 4 5 6 7 |
git revert -m 1 merge-commit-hash # -m 1 = keep main parent's changes |
Create revert without committing (inspect first)
|
0 1 2 3 4 5 6 7 |
git revert --no-commit abc1234 # now changes are staged → review → commit manually |
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! 🚀
