Command guide

git revert: Undo a Commit Without Rewriting History

By Admin · Updated · 4 min read · Checked against the official Git docs

git revert <commit> undoes a commit by adding a new commit that does the opposite. The original stays in history. Because nothing is rewritten, it’s the right way to undo something on a branch other people have already pulled, like main.

Quick answer

Find the hash with git log --oneline, run git revert <hash>, then push. For a merge commit, add -m 1.

git revert creates a new commit that undoes an earlier one

How we tested: every command on this page was run in a real Git repository (Git 2.43) on 18 November 2026, and the output shown is copied from the terminal. Your commit hashes will differ.

revert or reset?

git revert git reset
What it does Adds a new commit that cancels the old one Moves the branch back, dropping commits
History Kept Rewritten
Safe after pushing? Yes No (needs a force push)
Can undo a commit in the middle? Yes, just that one Only by dropping everything after it too

Rule of thumb: local and unpushed, reset. Pushed and shared, revert.

Revert one commit

We want to pull the coupon feature back out, but keep the checkout work that came after it:

$ git log --oneline
efa7454 Add checkout
07f154b Add coupon codes
2010b52 Add cart
0c04d29 Add homepage
$ git revert --no-edit 07f154b
[main 38ef07c] Revert "Add coupon codes"
 Date: Wed Nov 18 11:13:00 2026 +0000
 1 file changed, 1 deletion(-)
 delete mode 100644 coupon.js
$ git log --oneline -2
38ef07c Revert "Add coupon codes"
efa7454 Add checkout
$ ls
app.js
cart.js
checkout.js
index.html

coupon.js is gone and checkout.js is untouched. Without --no-edit, Git opens your editor with a message like Revert "Add coupon codes". It’s worth adding a line saying why; future you will want to know.

To revert the latest commit, you don’t need the hash: git revert HEAD.

Revert several commits as one

By default, reverting a range creates one revert commit per commit. To get a single commit instead, use --no-commit, check the result, then commit yourself:

$ git revert --no-commit HEAD~2..HEAD
$ git status --short
D  checkout.js
A  coupon.js
$ git revert --abort

The last two commits in our repo were “Add checkout” and the coupon revert from above, so reversing both deletes checkout.js and brings coupon.js back, exactly what the status shows. We only wanted to look, so we backed out with --abort. To keep the result, run git commit -m "Revert checkout and coupon revert" instead. Note the range syntax: HEAD~2..HEAD means “the last two commits”, not three; the commit on the left of .. is excluded.

Revert a merge commit

A plain revert fails on a merge commit, because Git doesn’t know which side to go back to:

$ git revert HEAD
error: commit 178102b0dedbe0fdf188f03f585dce6c82e27b53 is a merge but no -m option was given.
fatal: revert failed

-m 1 means “keep the first parent”, which is the branch you merged into (usually main). That undoes everything the merged branch brought in:

$ git revert -m 1 --no-edit HEAD
[main e1b9661] Revert "Merge branch 'feature/search'"
 Date: Wed Nov 18 11:25:00 2026 +0000
 1 file changed, 1 deletion(-)
 delete mode 100644 search.js
$ git log --oneline -3
e1b9661 Revert "Merge branch 'feature/search'"
178102b Merge branch 'feature/search'
d9ad807 Add search
Re-merging later won’t bring those changes back. Git sees the branch’s commits as already merged. When the feature is fixed and ready again, revert the revert commit first (git revert <hash-of-revert>), then merge the new fixes.

When a revert conflicts

If later commits changed the same lines, Git can’t reverse the old commit cleanly and stops:

$ git revert HEAD~1
Auto-merging price.txt
CONFLICT (content): Merge conflict in price.txt
error: could not revert 8341517... Raise price
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git revert --continue".
hint: You can instead skip this commit with "git revert --skip".
hint: To abort and get back to the state before "git revert",
hint: run "git revert --abort".

You have three ways out, exactly as the hints say. Fix the conflicted file, git add it, and run git revert --continue. Or skip this commit with --skip. Or cancel the whole thing:

$ git revert --abort
$ git status --short

An empty status means you’re back where you started. Our merge conflict guide shows how to read and fix the conflict markers.

FAQ

Does git revert delete the commit?

No. The original commit stays in history. Revert adds a new commit that applies the opposite changes.

How do I revert the last commit?

Run git revert HEAD, then push the new commit.

How do I revert a merge commit?

Run git revert -m 1 <merge-hash>. The 1 keeps the branch you merged into and removes what the other branch added.

Can I undo a git revert?

Yes. Revert the revert commit: git revert <revert-commit-hash> brings the original changes back.

What does git revert –no-commit do?

It applies the reversed changes to your files and staging area without committing, so you can combine several reverts into one commit or inspect the result first.

Written by Admin

Writes practical Git guides for Git Commands. Every command is checked against the official Git documentation.

About Git Commands