git revert: undo a commit with a new commit

Updated July 31, 2026

git revert creates a new commit that undoes the changes of the one you name, while the commit being undone stays in the history right where it was. That is how you roll back work that already went to the server: the history is not rewritten, so nothing falls apart for the people who pulled the branch.

$ git revert 0da918c
[main 375d973] Revert "fixed the typos"
 1 file changed, 1 deletion(-)

$ git log --oneline
375d973 Revert "fixed the typos"
0da918c fixed the typos
435eda7 added the hiring section

Both commits are in the history now: the one that broke things and the one that undid it. Before making the commit git opens your editor with a prepared message, Revert "...", and saving it is enough. The --no-edit flag skips that step.

The difference from git reset is the direction of travel. Reset moves the branch pointer back and commits drop out of the history. Revert adds a commit forward and leaves the history alone. Work that is only on your machine: reset is fine. Work that is on the server: revert only.

You can undo several commits at once, or a whole merge:

$ git revert HEAD~3..HEAD    # the last three commits, newest first
$ git revert -m 1 9af7538    # undo a merge, going back to the receiving branch

A merge commit has two parents, and git cannot tell which of the two states counts as "before". Without a parent named, it refuses to run:

$ git revert 9af7538
error: commit 9af7538c1e0b4a2d6f83b5c07e19d4a3f2b86c15 is a merge but no -m option was given.
fatal: revert failed

-m 1 names the first parent, the branch you merged into. That is the state you get back.

Common mistakes

An undone merge leaves a mark. Git still counts the merged commits as applied, so merging the same branch again will not bring them back. To get them back you undo the undo: git revert <hash>, where the hash is that Revert "Merge branch ..." commit.

Second: revert undoes specific commits, it does not "return the project to how it looked last week". For several commits, give a range and mind the order. Git applies the undos from newest to oldest, because otherwise they would not stack on top of each other.

Where this is in the book

The choice between revert and reset is covered in chapter 8, merging branches in chapter 5.

Chapter 8. First aid: when something goes wrong

To practise it by hand, open the Sandbox: a real terminal with git and step checks.