How to use Git Revert
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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:
If 4c39def is wrong, you can revert it:
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 revertto undo history without rewriting it - use
git resetonly when you intentionally want to rewrite history
Reverting A Single Commit
The most common case is reverting one known commit.
By default, Git opens an editor for the new revert commit message. If you want the default message without editing, use:
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:
If you want to stage the reverse patches first and commit once at the end, use --no-commit:
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.
-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:
If you decide to stop the operation:
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
This says:
- identify the bad commit
- create a new commit that undoes it
- 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 revertwhen 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 revertto 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 revertcreates a new commit that undoes an earlier commit.' - It is the preferred rollback tool for shared or public history.
- Use
--no-editfor a quick revert and--no-commitwhen 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
- How to use git to get just the latest revision of a project?
- How to use Git's default commit message when resolving merge conflicts?
- How to use Merge layer concat function on Keras 2.0.0?
- How to use Rabbit inside a gitlab-ci.yml file?
- How to use submodules publicly, but symlinks to a single clone locally?
- How to version control a source code which communicates with database?
- How to view diff of a forked github project
- How to view file diff in git before commit
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.