git revert
Intermediate Git CLI Safe
Summary: git revert <hash> creates a new commit whose content is the opposite of the commit you name. Nothing is removed from history, which is what makes it the only safe undo for work other people already have.
Safe — it adds a commit rather than rewriting any, so everyone else's clone stays in agreement with yours.
What it does
Works out the changes introduced by the named commit, applies them backwards to your working tree, and commits the result. The original commit stays exactly where it was.
Why it exists
Once a commit is pushed, other people have it. Removing it locally would make your branch disagree with theirs, and publishing that would need a force push. Revert undoes the effect without touching the history.
When to use it
- A pushed commit turns out to be wrong.
- A release must be backed out quickly, with a record of what happened.
- You want the mistake and its correction both visible, which is usually what an audit or an incident review wants.
When not to use it
- On unpushed commits, where
git resetorgit commit --amendgive a tidier history. - To "go back to how it was three commits ago": revert undoes one commit at a time. For a range,
git revert <oldest>..<newest>.
Syntax
| Form | Effect |
|---|---|
git revert <hash> |
Revert one commit, opening an editor for the message |
git revert --no-edit <hash> |
Same, accepting the default message |
git revert -n <hash> |
Apply the reversal but do not commit yet, so several can be combined |
git revert -m 1 <merge hash> |
Revert a merge, keeping the first parent (the branch merged into) |
git revert <a>..<b> |
Revert a range of commits |
git revert --abort |
Stop, if the revert hit a conflict |
Examples
Undo a pushed commit:
$ git revert 97fc096[main 4bd6eb3] Revert "docs: add Windows section"
1 file changed, 2 deletions(-)$ git log --oneline -34bd6eb3 Revert "docs: add Windows section"
97fc096 docs: add Windows section
d3b92ec chore: add CI configuration and contributing guide
Both commits are present: the change and its undo.
Reverting a merge needs -m, and says so:
$ git revert HEADerror: commit 85fd3d5f2a165b6f0e3bc4f9c9fb7c18a7ef5174 is a merge but no -m option was given.
fatal: revert failed$ git revert -m 1 HEAD[main 0abc660] Revert "Merge branch 'docs/licence-wording'"
1 file changed, 1 insertion(+), 1 deletion(-)
Reverting an older commit removes what it added:
$ git revert --no-edit 28593d2[main 91a7d9e] Revert "feat: add the trail list command"
3 files changed, 103 deletions(-)
delete mode 100644 data/trails.csv
delete mode 100644 src/trailguide.py
delete mode 100644 tests/test_trailguide.pyExpected result
A new commit on top of your branch, named Revert "<original subject>", and files matching the state before the reverted change. Push it like any other commit.
Try it here
The simulator below runs a small model of Git in the page. Nothing here touches a real repository, so try the command, then try it wrong (lesson 2.8 explains it).
The Git state simulator loads here.
Common mistakes
- Expecting the original commit to disappear. It stays; that is the point.
- Forgetting
-m 1on a merge, then not reading the error, which names the missing option. - Reverting a merge and later re-merging the branch, which does not bring the changes back: Git considers them merged already. The fix is to revert the revert.
- Reverting a range in the wrong order. Revert newest first, or use the range form which handles it.
- Using revert on unpushed commits, which leaves two commits where none was needed.
How to undo or recover
A revert is an ordinary commit, so revert the revert: git revert <revert hash>. If it has not been pushed, git reset --hard HEAD~1 also works.
In VS Code
Right-click a commit in the Source Control graph or the Timeline and choose Revert Commit.
In IntelliJ IDEA
Right-click a commit in the Log → Revert Commit. IntelliJ opens the commit dialog so you can edit the message.
On GitLab and GitHub
Both offer a Revert button on a merged merge request or pull request, which creates the revert commit on a new branch and opens a merge or pull request for it. That is the recommended route for anything on a protected branch.
Related commands
git reset for unpushed commits · git cherry-pick, its mirror image · git log to find the hash · git revert --abort to escape a conflicting revert.
Lessons that use this command
The undo map · restore vs reset vs revert vs rebase · Recover from a failed merge or rebase.