Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Merging and Rebasing

git revert command

Reference for git revert: undo one or more commits by adding new commits, revert merge commits with -m, use --no-commit, handle conflicts and re-apply a revert.

git revert undoes the effect of an earlier commit by adding a new commit that applies the opposite change. History is not rewritten: the original commit and its revert both stay in the log, which makes revert the safe way to undo something that has already been pushed or shared.

Compare this with git reset, which moves a branch back and makes commits disappear from it. Use reset for local work nobody else has seen; use revert when others may have pulled the commit. Git builds the revert with a three-way merge, so it can still undo an old commit even if later commits touched nearby lines, and it can stop with a conflict.

Terminal
git revert 3f2a1b9            # new commit that undoes 3f2a1b9
git revert HEAD~2..HEAD       # revert the last two commits, one by one
git revert -m 1 <merge>       # revert a merge, keeping parent 1 as mainline

Try it

Bash Revert a commit, a range and a no-commit revert

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Reverting a merge and handling a conflict

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git revert [<options>] <commit>...
git revert (--continue | --skip | --abort | --quit)
Name Description
<commit>... Commits to undo. A range A..B reverts every commit after A up to B, newest first.
-n, --no-commit Apply the undo to the index and working tree but do not commit. Several reverts then become one commit.
-e, --edit / --no-edit Open or skip the message editor. The default message is Revert "<original subject>" plus the original id.
-m <n>, --mainline <n> Required for merge commits: which parent (1-based) is the line to keep.
-s, --signoff Add a Signed-off-by line.
-X <option>, --strategy Merge strategy options, see Merge strategies.
--reference Use the <id> (<subject>, <date>) reference form in the message.
--continue, --skip, --abort, --quit Control a stopped sequence of reverts.

Errors and edge cases: reverting a merge without -m fails with is a merge but no -m option was given. A dirty index or conflicting later changes stop the revert and leave markers to resolve.

Usage notes

  • Safe for shared history. Nobody has to rewrite anything; the pushed revert is just another commit. This is the right undo for main.
  • Several commits are reverted separately, in reverse order, giving one revert commit each. Use -n to fold them into a single commit.
  • Reverting a merge is not the end of the story. -m 1 removes the merged branch's changes, but Git still records that those commits were merged. Merging the same branch again later brings nothing back. To re-merge it you must first revert the revert (or recreate the commits).
  • Order of conflicts. Reverting an old commit can conflict with newer work on the same lines. Resolve, git add, then git revert --continue.
  • Not the same as restore. To bring a single file back to an older state without a history entry for the undo, use git restore --source=<commit> -- <file>.
  • Revert of a revert restores the original change, as the first example shows.
  • Empty reverts. If the change is already undone, Git reports that the revert is empty; --skip moves on.
  • Pending rework. If you still want the commit's idea later, revert it now and cherry-pick the commit again after the fix; see git cherry-pick.
  • Recorded in the log. git log --grep='^Revert' lists them; the message names the commit that was undone.