🌐
Git Scripts
gitscripts.com › git-revert-m-1
Mastering Git Revert -m 1 for Effortless Undoing
March 28, 2025 - The `git revert -m 1` command is used to undo a merge commit by specifying which parent (in this case, the first parent) to be considered as the mainline, effectively reverting to the state before the merge was made.
🌐
Git
git-scm.com › docs › git-revert
Git - git-revert Documentation
This requires your working tree to be clean (no modifications from the HEAD commit). Note: git revert is used to record some new commits to reverse the effect of some earlier commits (often only a faulty one). If you want to throw away all uncommitted changes in your working directory, you should see git-reset[1...
Discussions

github - Why does git revert complain about a missing -m option? - Stack Overflow
Often this will be parent number ... to revert the merge of unwanted. The first parent would be your pre-merge master branch and the second parent would be the tip of unwanted. ... git cat-file -p [MERGE_COMMIT_ID] will show the parent branches in order. The first one listed would be -m 1, the second ... More on stackoverflow.com
🌐 stackoverflow.com
git checkout - How do I revert a Git repository to a previous commit? - Stack Overflow
You can also use a range of merge commits here. git revert -m 1 # To get just one, you could use `rebase -i` to squash them afterwards # Or, you could do it manually (be sure to do this at top level of the repo) # get your index and work tree into the desired state, without ... More on stackoverflow.com
🌐 stackoverflow.com
How do I undo a revert in Git? - Stack Overflow
I've been working on a large project. I made an initial commit and didn't commit again for a while. My project is almost done so I decided to mess around with git again and commit the new files in my More on stackoverflow.com
🌐 stackoverflow.com
How to use Git Revert - Stack Overflow
It leaves the files in the same ... has been reverted never existed. For example, consider the following simple example: $ cd /tmp/example $ git init Initialized empty Git repository in /tmp/example/.git/ $ echo "Initial text" > README.md $ git add README.md $ git commit -m "initial commit" [master (root-commit) 3f7522e] initial commit 1 file changed, ... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

What is the difference between git revert and git reset?
`git revert` creates a new commit that undoes changes while preserving the full commit history. `git reset` moves the HEAD pointer backward and can remove commits from the history. Use `git revert` for shared branches and `git reset` for local, unpushed changes.
🌐
linuxize.com
linuxize.com › home › git › how to revert a commit in git
How to Revert a Commit in Git | Linuxize
Can I undo a git revert?
Yes. Since a revert is just a regular commit, you can revert the revert commit itself: `git revert REVERT_COMMIT_HASH`. This effectively re-applies the original changes.
🌐
linuxize.com
linuxize.com › home › git › how to revert a commit in git
How to Revert a Commit in Git | Linuxize
How do I revert a merge commit?
Use the `-m` option to specify the parent number. For example, `git revert -m 1 MERGE_HASH` reverts the merge and keeps the first parent (usually the main branch) as the base.
🌐
linuxize.com
linuxize.com › home › git › how to revert a commit in git
How to Revert a Commit in Git | Linuxize
🌐
DataCamp
datacamp.com › tutorial › git-revert-merge
Git Revert Merge Commit: A Guide With Examples | DataCamp
November 5, 2024 - git revert -m 1 <merge_commit_hash> git push origin <branch_name>
🌐
Git
git-scm.com › docs › git › 2.40.2
Git - git Documentation
These commands are to interact with foreign SCM and with other people via patch over e-mail. ... There are three commands with similar names: git reset, git restore and git revert. git-revert[1] is about making a new commit that reverts the changes made by other commits.
🌐
CloudBees
cloudbees.com › blog › git-revert-explained
Git Revert Explained: Safely Undoing Your Changes
November 29, 2021 - Instead, it inverts the changes implemented in a commit and appends new commits with the opposite effect. This process helps Git remove the unwanted commit from the codebase and retain the history of every commit and the reverted one.
🌐
Devart
devart.com › dbforge › sql › source-control › reverting-git-commit-with-examples.html
Reverting a Git Commit with Examples - Devart
It allows you to invert the committed changes from an earlier single commit in a new commit. It means that Git does not revert the content to the previous state, it removes the changes from the specified commit.
🌐
freeCodeCamp
freecodecamp.org › news › git-revert-commit-how-to-undo-the-last-commit
Git Revert Commit – How to Undo the Last Commit
August 31, 2021 - Then you can copy from there the alphanumerical name and use that in the revert command. In this image, each circe represents a commit. You can also use the reset command to undo your last commit. But be careful – it will change the commit history, so you should use it rarely. It will move the HEAD, the working branch, to the indicated commit, and discard anything after: git reset --soft HEAD~1 ·
Find elsewhere
🌐
Linuxize
linuxize.com › home › git › how to revert a commit in git
How to Revert a Commit in Git | Linuxize
May 23, 2026 - How do I revert a merge commit? Use the -m option to specify the parent number. For example, git revert -m 1 MERGE_HASH reverts the merge and keeps the first parent (usually the main branch) as the base.
🌐
Atlassian
atlassian.com › git › tutorials › resetting checking out and reverting
Resetting, Checking Out & Reverting | Atlassian Git Tutorial
December 16, 2025 - A file level checkout will change the file's contents to those of the specific commit. A revert is an operation that takes a specified commit and creates a new commit which inverses the specified commit.
🌐
Linux Kernel
kernel.org › pub › software › scm › git › docs › git-revert.html
git-revert(1) Manual Page
June 20, 2025 - This requires your working tree to be clean (no modifications from the HEAD commit). Note: git revert is used to record some new commits to reverse the effect of some earlier commits (often only a faulty one). If you want to throw away all uncommitted changes in your working directory, you should see git-reset(1...
🌐
Opensource.com
opensource.com › article › 18 › 6 › git-reset-revert-rebase-commands
How to reset, revert, and return to previous states in Git | Opensource.com
If we add a line to a file in each commit in the chain, one way to get back to the version with only two lines is to reset to that commit, i.e., git reset HEAD~1. Another way to end up with the two-line version is to add a new commit that has ...
Top answer
1 of 4
397

By default git revert refuses to revert a merge commit as what that actually means is ambiguous. I presume that your HEAD is in fact a merge commit.

If you want to revert the merge commit, you have to specify which parent of the merge you want to consider to be the main trunk, i.e. what you want to revert to.

Often this will be parent number one, for example if you were on master and did git merge unwanted and then decided to revert the merge of unwanted. The first parent would be your pre-merge master branch and the second parent would be the tip of unwanted.

In this case you could do:

git revert -m 1 HEAD

git cat-file -p [MERGE_COMMIT_ID] will show the parent branches in order. The first one listed would be -m 1, the second -m 2.

2 of 4
62

Say the other guy created bar on top of foo, but you created baz in the meantime and then merged, giving a history of

$ git lola
*   2582152 (HEAD, master) Merge branch 'otherguy'
|\  
| * c7256de (otherguy) bar
* | b7e7176 baz
|/  
* 9968f79 foo

Note: git lola is a non-standard but useful alias.

No dice with git revert:

$ git revert HEAD
fatal: Commit 2582152... is a merge but no -m option was given.

Charles Bailey gave an excellent answer as usual. Using git revert as in

$ git revert --no-edit -m 1 HEAD
[master e900aad] Revert "Merge branch 'otherguy'"
 0 files changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 bar

effectively deletes bar and produces a history of

$ git lola
* e900aad (HEAD, master) Revert "Merge branch 'otherguy'"
*   2582152 Merge branch 'otherguy'
|\  
| * c7256de (otherguy) bar
* | b7e7176 baz
|/  
* 9968f79 foo

But I suspect you want to throw away the merge commit:

$ git reset --hard HEAD^
HEAD is now at b7e7176 baz

$ git lola
* b7e7176 (HEAD, master) baz
| * c7256de (otherguy) bar
|/  
* 9968f79 foo

As documented in the git rev-parse manual

<rev>^, e.g. HEAD^, v1.5.1^0
A suffix ^ to a revision parameter means the first parent of that commit object. ^<n> means the n-th parent (i.e. <rev>^ is equivalent to <rev>^1). As a special rule, <rev>^0 means the commit itself and is used when <rev> is the object name of a tag object that refers to a commit object.

so before invoking git reset, HEAD^ (or HEAD^1) was b7e7176 and HEAD^2 was c7256de, i.e., respectively the first and second parents of the merge commit.

Be careful with git reset --hard because it can destroy work.

Top answer
1 of 16
12602

This depends a lot on what you mean by "revert".

Temporarily switch to a different commit

If you want to temporarily go back to it, fool around, then come back to where you are, all you have to do is check out the desired commit:

# This will detach your HEAD, that is, leave you with no branch checked out:
git checkout 0d1d7fc32

Or if you want to make commits while you're there, go ahead and make a new branch while you're at it:

git checkout -b old-state 0d1d7fc32

To go back to where you were, just check out the branch you were on again. (If you've made changes, as always when switching branches, you'll have to deal with them as appropriate. You could reset to throw them away; you could stash, checkout, stash pop to take them with you; you could commit them to a branch there if you want a branch there.)

Hard delete unpublished commits

If, on the other hand, you want to really get rid of everything you've done since then, there are two possibilities. One, if you haven't published any of these commits, simply reset:

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts, if you've modified things which were
# changed since the commit you reset to.

If you mess up, you've already thrown away your local changes, but you can at least get back to where you were before by resetting again.

Undo published commits with new commits

On the other hand, if you've published the work, you probably don't want to reset the branch, since that's effectively rewriting history. In that case, you could indeed revert the commits. In many enterprise organisations, the concept of "protected" branches will even prevent history from being rewritten on some major branches. In this case, reverting is your only option.

With Git, revert has a very specific meaning: create a commit with the reverse patch to cancel it out. This way you don't rewrite any history.

First figure out what commits to revert. Depending on the technique chosen below, you want to either revert only the merge commits, or only the non-merge commits.

# This lists all merge commits between 0d1d7fc and HEAD:
git log --merges --pretty=format:"%h" 0d1d7fc..HEAD | tr '\n' ' '

# This lists all non merge commits between 0d1d7fc and HEAD:
git log --no-merges --pretty=format:"%h" 0d1d7fc..HEAD | tr '\n' ' '

Note: if you revert multiple commits, the order matters. Start with the most recent commit.

# This will create three separate revert commits, use non merge commits only:
git revert a867b4af 25eee4ca 0766c053

# It also takes ranges. This will revert the last two commits:
git revert HEAD~2..HEAD

# Similarly, you can revert a range of commits using commit hashes (non inclusive of first hash):
git revert 0d1d7fc..a867b4a

# Reverting a merge commit. You can also use a range of merge commits here.
git revert -m 1 <merge_commit_sha>

# To get just one, you could use `rebase -i` to squash them afterwards
# Or, you could do it manually (be sure to do this at top level of the repo)
# get your index and work tree into the desired state, without changing HEAD:
git checkout 0d1d7fc32 .

# Then commit. Be sure and write a good message describing what you just did
git commit

The git-revert manpage actually covers a lot of this in its description. Another useful link is this git-scm.com section discussing git-revert.

If you decide you didn't want to revert after all, you can revert the revert (as described here) or reset back to before the revert (see the previous section).

You may also find this answer helpful in this case:
How can I move HEAD back to a previous location? (Detached head) & Undo commits

2 of 16
3981

Lots of complicated and dangerous answers here, but it's actually easy:

git revert --no-commit 0d1d7fc3..HEAD
git commit
git push

This will revert everything from the HEAD back to the commit hash (excluded), meaning it will recreate that commit state in the working tree as if every commit after 0d1d7fc3 had been walked back. You can then commit the current tree, and it will create a brand new commit essentially equivalent to the commit you "reverted" to.

(The --no-commit flag lets git revert all the commits at once- otherwise you'll be prompted for a message for each commit in the range, littering your history with unnecessary new commits.)

This is a safe and easy way to rollback to a previous state. No history is destroyed, so it can be used for commits that have already been made public.


Note on merge commits:
If one of the commits between 0766c053..HEAD (inclusive) is a merge then there will be an error popping up (to do with no -m specified). The following link may help those encountering that: Why does git revert complain about a missing -m option? (thanks @timhc22 for pointing out)

🌐
PhoenixNAP
phoenixnap.com › home › kb › sysadmin › git revert commit: how to undo last commit
Git Revert Commit: How to Revert Previous Commit
February 4, 2026 - The system asks you to enter a specific commit message for the changes the revert command is going to perform. This action creates a new commit based on the one you specified, with a revert tag. This acts as a log, showing that the commit was published and then reverted (instead of pretending it never happened). Note: Learn how to use Git cherry-pick to select and apply a single commit from one branch to another.
🌐
DEV Community
dev.to › gabeguz › git-revert-merge-commits-confusion
Git Revert Merge: git revert, merge commits, confusion - DEV Community
September 8, 2017 - So: You create a merge: git merge <feature> You revert that (creating a new commit, say, <revmerge>): git revert -m 1 <mergecommit>
🌐
DEV Community
dev.to › womakerscode › tutorial-git-desfazendo-commits-revert-57c2
[Tutorial Git] git revert: Desfazendo commits - DEV Community
April 14, 2021 - O que acontece aqui, na verdade, é que o Git cria um novo commit que registra o que foi desfeito. Por exemplo: se em um commit você adicionou um arquivo, o revert remove; se o commit editou algumas linhas, o revert volta essas linhas ao que ...
Top answer
1 of 7
389

git revert makes a new commit

git revert simply creates a new commit that is the opposite of an existing commit.

It leaves the files in the same state as if the commit that has been reverted never existed. For example, consider the following simple example:

$ cd /tmp/example
$ git init
Initialized empty Git repository in /tmp/example/.git/
$ echo "Initial text" > README.md
$ git add README.md
$ git commit -m "initial commit"
[master (root-commit) 3f7522e] initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
$ echo "bad update" > README.md 
$ git commit -am "bad update"
[master a1b9870] bad update
 1 file changed, 1 insertion(+), 1 deletion(-)

In this example the commit history has two commits and the last one is a mistake. Using git revert:

$ git revert HEAD
[master 1db4eeb] Revert "bad update"
 1 file changed, 1 insertion(+), 1 deletion(-)

There will be 3 commits in the log:

$ git log --oneline
1db4eeb Revert "bad update"
a1b9870 bad update
3f7522e initial commit

So there is a consistent history of what has happened, yet the files are as if the bad update never occurred:

$ cat README.md 
Initial text

It doesn't matter where in the history the commit to be reverted is (in the above example, the last commit is reverted - any commit can be reverted).

Closing questions

do you have to do something else after?

A git revert is just another commit, so e.g. push to the remote so that other users can pull/fetch/merge the changes and you're done.

Do you have to commit the changes revert made or does revert directly commit to the repo?

git revert is a commit - there are no extra steps assuming reverting a single commit is what you wanted to do.

Obviously, you'll need to push again and probably announce your balls-up to the team.

Indeed - if the remote is in an unstable state - communicating to the rest of the team that they need to pull to get the fix (the reverting commit) would be the right thing to do :).

2 of 7
136

Use Git revert like so:

git revert <insert bad commit hash here>

git revert creates a new commit with the changes that are rolled back. git reset erases your Git history instead of making a new commit.

The steps after are the same as any other commit.

🌐
GitHub
github.com › kodekloudhub › git-for-beginners-course › blob › master › docs › 06-Resetting-and-Reverting › 01-Resetting-and-Reverting.md
git-for-beginners-course/docs/06-Resetting-and-Reverting/01-Resetting-and-Reverting.md at master · kodekloudhub/git-for-beginners-course
A git revert command is useful if you want to undo the changes and keep those changes in your GIT history. ... In the git reset command, there is two ways to reset the commit. Either with the --soft flag which we wants to keep the changes that ...
Author   kodekloudhub
🌐
Reddit
reddit.com › r/git › how do you revert the first commit to a repo when it is the only commit ?
r/git on Reddit: How do you revert the first commit to a repo when it is the only commit ?
July 25, 2024 - If you really want to "revert" the commit, i.e. create a new commit that reverses the changes of the previous commit, then git revert HEAD will do the job, regardless of whether the commit you're reverting is the only one in the repo.