GIT – Revert?

Saurabh Sharma
Image generated via Gemini. Indian man had to come out as a man wearning Dhoti! :)

Most of the blogs I write serve as a self reminder; things I have tried personally. In one of the recent encounters where a GUI tool wasn’t was possibility I was extra cautious while trying to roll back a production code that I had not committed to ensure no functionality gets broken.

This blog serves as a step-by-step reference guide for rolling back changes on branches that are already pushed to a remote.

The Golden Rule

If the branch is already pushed – use git revert. Never git reset --hard.

– Saurabh

git revert adds a new commit that undoes your changes. History is preserved, no force push is needed, and collaborators are not affected.

git reset --hard + git push --force rewrites history. It is dangerous on shared branches and should only ever be used on branches that are 100% private and local.

Quick Reference Card

Step-by-Step Walkthrough

Step 1 — Know where you are

git log --oneline -15 shows recent commits in compact form. Note down the hashes you want to undo.

git status confirms the working tree is clean before you start.

git branch -vv shows whether your branch is tracking a remote and whether it is ahead or behind. This tells you if the branch has already been pushed.

Example output:

Step 2 — Understand what each commit changed

Before reverting anything, read the commits. Never revert blindly.

Step 3 — Stage the revert without committing

This is the key safety move. The --no-commit flag applies the revert to the staging area but does not create a commit. You get a chance to inspect and abort before anything is permanent.

Understanding the range HEAD~N..HEAD:

The .. notation means the last N commits.

You want to revertUse this range
Last 1 commitHEAD~1..HEAD
Last 2 commitsHEAD~2..HEAD
Last 3 commitsHEAD~3..HEAD
A specific commitgit revert --no-commit <hash>

Common mistake: HEAD~1..HEAD only covers the very last commit – not two commits. Use HEAD~2..HEAD when you mean two commits back.

Git reverts commits in newest-first order automatically, which is the correct way to unwind a stack of changes.

Step 4 — Validate before committing

This is the most important part of the process. Never skip it.

The gold-standard check — compare the staged result directly against the commit you are targeting:

If this command produces empty output, the staged state is byte-for-byte identical to that commit. The revert is perfect.

Step 5a — Commit when satisfied

Git opens your editor with a pre-filled commit message. You can edit it or accept the default. Save and close to finalize the commit.

Step 5b — Abort if something looks wrong

This cancels everything and returns the repo to a clean state. No changes are made. Use this freely; it is completely safe.

Step 6 — Push

Because git revert preserves history, this is a plain push with no flags needed. The remote will accept it cleanly.

Why Not git reset?

Descriptiongit revertgit reset --hard + force push
Rewrites historyNoYes
Safe on pushed branchesYesNo
Needs --force pushNoYes
UndoableYes (revert the revert)Very difficult
Affects collaboratorsNoYes — breaks their local branches

The only situation where git reset --hard is acceptable is on a branch that has never been pushed and that only you are using.

Cheat Sheet: Diagnosing the Range

If you are unsure what a range covers, run this before reverting:

The output shows exactly which commits will be undone. Confirm this matches your intention before proceeding.

Full Example

Remembering It All

Think of the process as: Look → Understand → Stage → Validate → Commit → Push

A practical guide to safe, history-preserving git rollbacks.

Leave a Reply