Git Revert
Version Control
Git Commands
Software Development
Git Tutorial

How to use Git Revert

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

git revert is the safest way to undo a committed change when the history is already shared or should remain intact. Instead of deleting commits, it creates a new commit that applies the inverse patch of an earlier one. That makes it very different from git reset, which rewrites branch history.

What git revert Actually Does

Suppose this commit introduced a bug:

bash
git log --oneline -3
text
8f12abc Fix invoice formatting
4c39def Add discount calculation
1a22bbb Initial pricing logic

If 4c39def is wrong, you can revert it:

bash
git revert 4c39def

Git computes the inverse of that commit's changes and records a new commit. The original buggy commit stays in history, which is exactly why revert is safe on shared branches.

This is the core rule:

  • use git revert to undo history without rewriting it
  • use git reset only when you intentionally want to rewrite history

Reverting A Single Commit

The most common case is reverting one known commit.

bash
git revert 4c39def

By default, Git opens an editor for the new revert commit message. If you want the default message without editing, use:

bash
git revert --no-edit 4c39def

That new commit will usually be titled something like Revert "Add discount calculation".

Reverting Several Commits

You can revert multiple commits, but be careful about order and dependencies.

For a small explicit list:

bash
git revert commitA commitB

If you want to stage the reverse patches first and commit once at the end, use --no-commit:

bash
git revert --no-commit commitA commitB
git commit -m "Revert two related pricing changes"

This is useful when several bad commits should be undone as one logical rollback.

Reverting A Merge Commit

Merge reverts are special. Git's documentation notes that you usually cannot revert a merge without telling Git which parent is the mainline. That is what -m is for.

bash
git revert -m 1 <merge-commit>

-m 1 means "treat parent 1 as the line of history to keep." If you merged a topic branch into main, parent 1 is usually the branch you were on when you ran the merge.

This is not just syntax trivia. Reverting a merge affects how Git reasons about future merges, so you should only do it when you understand which side of the merge should remain the mainline.

Conflicts Can Still Happen

A revert is a normal patch application, so it can conflict if later commits changed the same lines.

Typical flow:

bash
1git revert 4c39def
2# resolve conflicts in files
3# git add resolved files
4git revert --continue

If you decide to stop the operation:

bash
git revert --abort

That looks similar to merge or rebase conflict handling because under the hood Git is trying to apply a patch cleanly against the current tree.

Why git revert Is Better Than git reset On Shared Branches

Imagine a bad commit is already on main and teammates have pulled it. If you use git reset --hard and force-push, everyone else now has rewritten history to deal with. If you use git revert, the branch history stays linear and everyone can pull the fix normally.

That is why teams often prefer git revert for production rollbacks and public branches.

A Practical Example Workflow

bash
git log --oneline
git revert --no-edit 4c39def
git push origin main

This says:

  1. identify the bad commit
  2. create a new commit that undoes it
  3. push the rollback like any other change

Simple and safe.

When Not To Use git revert

git revert is the wrong tool if the change is only in your working tree and has not been committed yet. In that case, use git restore or git reset depending on whether the change is unstaged or staged.

It is also not the best tool when you want to edit history on a private branch before sharing it. There, interactive rebase or git commit --amend may be cleaner.

Common Pitfalls

  • Using git revert when the problem is only an uncommitted local file change.
  • Reverting a merge commit without specifying the correct mainline parent.
  • Reverting several dependent commits in a careless order and creating unnecessary conflicts.
  • Expecting git revert to erase history. It adds history; it does not remove it.
  • Force-pushing a reset on a shared branch when a revert would have been safer.

Summary

  • 'git revert creates a new commit that undoes an earlier commit.'
  • It is the preferred rollback tool for shared or public history.
  • Use --no-edit for a quick revert and --no-commit when batching multiple reversions.
  • Merge commits usually require -m <parent-number>.
  • Conflicts are possible, so be ready to resolve them and continue or abort the revert.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.