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 :).

Answer from AD7six on Stack Overflow
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.

🌐
TheServerSide
theserverside.com › tutorial › How-to-git-revert-a-commit-A-simple-undo-changes-example
How to revert a Git commit: A simple example | TheServerSide
The git revert command will undo only the changes associated with a specific commit. In this git revert example, the third commit added the charlie.html file.
Discussions

Using Git, what happens if I revert a commit that was made prior to several other commits? Will it undo all the subsequent commits?
Good news: once something is committed in Git, you're unlikely to lose it unless you really go out of your way! So I admire that you're taking the safe route because you're afraid of losing data, but as you get to understand Git better, hopefully you can trust it! In Git, a "revert" means to make a NEW commit that undoes a previous commit. The entire previous history is still there. Let's say I make three commits: A: append "It was the best of times, it was the worst of times" B: append "it was the age of wisdom, it was the age of foolishness" C: append "it was the epoch of belief, it was the epoch of incredulity" At the end of this, my file looks like this: It was the best of times, it was the worst of times it was the age of wisdom, it was the age of foolishness it was the epoch of belief, it was the epoch of incredulity If I revert C, I'll end up with this history: A, B, C, revert-C My file will now look like this: It was the best of times, it was the worst of times it was the age of wisdom, it was the age of foolishness But that third line of text isn't gone. It's still there in my history. I can see it with "git diff" or "git show" or any number of other commands. Or I could check out a previous commit and create a new branch for it. You don't have to revert in order. Git reverts just one commit at a time. So if I revert A, my history will look like this: A, B, C, revert-A My file might look like this: it was the age of wisdom, it was the age of foolishness it was the epoch of belief, it was the epoch of incredulity However, trying to revert things out of order might fail. Git will be rightfully worried about a merge conflict and it might show you the diff and ask you to manually resolve it. If you wanted to undo all of those changes, you could: revert C revert B revert A Now you'd be right back where you started, but you'd still have all of your history. So...revert away! BTW, if you're curious, there is a totally different way to undo a few changes. If you instead ran: git reset --hard A, that would tell Git to reset back to when A was the only thing committed, and B and C had never been committed. That is erasing history! But, Git is nice and doesn't throw away B and C just yet. They can still be recovered if you made a mistake. So that's why you shouldn't worry! More on reddit.com
🌐 r/learnprogramming
5
6
February 18, 2022
git checkout - How do I revert a Git repository to a previous commit? - Stack Overflow
In the following example the head would be set back one commit, to the last commit in the repository history: Push the change to Git repository using git push --force to force push the change. If you want the Git repository to a previous commit:- git pull --all git reset --hard HEAD~1 git push --force ... Save this answer. ... Show activity on this post. Revert ... More on stackoverflow.com
🌐 stackoverflow.com
New to git, advice needed. Revert commit but keep changes
You can either reset, as u/large_crimson_canine suggested, or you can make your new changes, git add them and then amend the commit you already made with: git --amend or, to retain the previous commit message: git --amend --no-edit More on reddit.com
🌐 r/git
17
0
November 18, 2023
I'm struggling to understand how to roll back to a previous point. I want to disregard the crossed out chain completely. What am I missing here?
Edit: That was definitely it. I undid the new commit and created a new branch and it is acting as I was expecting now. I decided the last 3 commits I did were just not going to work, so I wanted to roll back to my v0.1.5a commit. So I "Undo Last Commit" a few times to get back there, then tried committing to a chain. I didn't actually create a branch, maybe that was what I did wrong? More on reddit.com
🌐 r/git
12
0
December 11, 2024
🌐
Git
git-scm.com › docs › git-revert.html
Git - git-revert Documentation
While git creates a basic commit message automatically, it is strongly recommended to explain why the original commit is being reverted. In addition, repeatedly reverting reverts will result in increasingly unwieldy subject lines, for example Reapply "Reapply "<original-subject>"".
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes › git revert
How to Revert a Commit in Git? | Atlassian Git Tutorial
Second, git revert is able to target an individual commit at an arbitrary point in the history, whereas git reset can only work backward from the current commit. For example, if you wanted to undo an old commit with git reset, you would have to remove all of the commits that occurred after the target commit, remove it, then re-commit all of the subsequent commits.
🌐
W3Schools
w3schools.com › git › git_revert.asp
Git Revert
Use git log --oneline to find the commit you want to undo. Use git revert HEAD --no-edit to create a new commit that reverses the changes.
🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
When you commit to your local repository with git commit, Git records your changes. Because you did not push to a remote repository yet, your changes are not public or shared with others. At this point, you can undo your changes. You can revert a commit while retaining the commit history. This example uses five commits A,B,C,D,E, which were committed in order: A-B-C-D-E.
Find elsewhere
🌐
Devart
devart.com › dbforge › sql › source-control › reverting-git-commit-with-examples.html
Reverting a Git Commit with Examples - Devart
Suppose that you decided to undo the committed changes for any reason. To do that, you can use the git revert command. 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.
🌐
Rosettacommons
docs.rosettacommons.org › docs › latest › internal_documentation › Reverting-a-Commit
Reverting a Commit
This give a manual page for git revert. Reading it, we see that we need to supply the parent number of the parent we want to keep with the -m flag. Note that this is different from the SHA1 hash. ... This gives a summary of the commit. Note the line "Merge: d41f7cf 12a020b" at the top.
🌐
CloudBees
cloudbees.com › blog › git-revert-commit
Git Revert Commit: Everything to Know About Going Back
November 29, 2021 - You use git revert to record some new commits to reverse the effect of some earlier commits (possibly faulty ones). It is an “undo” command, but technically it is much more than that. Git revert does not delete any commit in this project history. Instead, it inverts the changes implemented in a commit and appends new commits with the opposite effect.
🌐
Aviator
aviator.co › home › blog › how to git undo commit: methods and best practices
How to Git Undo Commit: Methods and Best Practices - Aviator Blog
February 10, 2025 - Git uses a three-step lifecycle to manage changes: the working directory, the staging area, and the commit history. Let’s break these down with examples and visuals. Working Directory: This is where your files live as you work on them. Changes here are not yet tracked by Git until you explicitly stage them. Let’s say you’re developing a new feature for a SaaS dashboard; adding or modifying files in your project folder changes the working directory. You can use git checkout to revert changes in the working directory or switch to a different commit.
🌐
Simplilearn
simplilearn.com › home › resources › devops › how to revert a commit in git: a step-by-step guide
How to Revert a Commit in Git: A Step-by-Step Guide
June 13, 2026 - Git revert is a command used to undo changes in a Git repository by creating a new commit that reverses the changes made by a previous commit.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Graphite
graphite.com › guides › how-to-use-git-revert
How to use git revert
Unlike other (potentially destructive) operations such as [git reset](<https://graphite.com/guides/git-reset>), which alters the underlying commit history, git revert creates a new commit that undoes the changes introduced by previous commits.
🌐
freeCodeCamp
freecodecamp.org › news › git-reverting-to-previous-commit-how-to-revert-to-last-commit
Git Reverting to Previous Commit – How to Revert to Last Commit
October 19, 2022 - To do that, run the command below: ... As you can see above, this command lists all your commits along with their IDs. To go back to the second commit, you run the git reset command followed by the commit ID.
🌐
Quora
quora.com › How-do-I-revert-a-specific-git-commit
How to revert a specific git commit - Quora
Note You may need to revert the individual commits in your pull request if either of the following is true. Reverting the pull request causes merge conflicts The original pull request was not originally merged on GitHub. For example, someone could have merged the pull request using a fast-forward ...
🌐
Sentry
sentry.io › sentry answers › git › revert a git repository to a previous commit
Revert a Git repository to a previous commit | Sentry
To revert multiple recent commits, you can specify a range, from oldest to newest. One new commit will be created for each reverted commit. git revert HEAD~3...HEAD # revert the last three commits
🌐
Reddit
reddit.com › r/learnprogramming › using git, what happens if i revert a commit that was made prior to several other commits? will it undo all the subsequent commits?
r/learnprogramming on Reddit: Using Git, what happens if I revert a commit that was made prior to several other commits? Will it undo all the subsequent commits?
February 18, 2022 -

Also, if I just revert my most recent commit, make changes, then decide I want to go back to before I did the revert, can I do that? I've been manually making a new branch every time I want to work on a previous version of my code because I've been afraid to lose my current work by doing a revert.

If it makes a difference, I'm working in Visual Studio, with their built-in support for GitHub, not the command line. When I want to go back to a previous version of my code, I go to branch history, right click, and click "new branch". I know that can't be the best way to do it, but I've had a hard time finding a good explanation of how reverts actually work in Git/GitHub and I don't want to accidentally lose my current work.

Top answer
1 of 1
29
Good news: once something is committed in Git, you're unlikely to lose it unless you really go out of your way! So I admire that you're taking the safe route because you're afraid of losing data, but as you get to understand Git better, hopefully you can trust it! In Git, a "revert" means to make a NEW commit that undoes a previous commit. The entire previous history is still there. Let's say I make three commits: A: append "It was the best of times, it was the worst of times" B: append "it was the age of wisdom, it was the age of foolishness" C: append "it was the epoch of belief, it was the epoch of incredulity" At the end of this, my file looks like this: It was the best of times, it was the worst of times it was the age of wisdom, it was the age of foolishness it was the epoch of belief, it was the epoch of incredulity If I revert C, I'll end up with this history: A, B, C, revert-C My file will now look like this: It was the best of times, it was the worst of times it was the age of wisdom, it was the age of foolishness But that third line of text isn't gone. It's still there in my history. I can see it with "git diff" or "git show" or any number of other commands. Or I could check out a previous commit and create a new branch for it. You don't have to revert in order. Git reverts just one commit at a time. So if I revert A, my history will look like this: A, B, C, revert-A My file might look like this: it was the age of wisdom, it was the age of foolishness it was the epoch of belief, it was the epoch of incredulity However, trying to revert things out of order might fail. Git will be rightfully worried about a merge conflict and it might show you the diff and ask you to manually resolve it. If you wanted to undo all of those changes, you could: revert C revert B revert A Now you'd be right back where you started, but you'd still have all of your history. So...revert away! BTW, if you're curious, there is a totally different way to undo a few changes. If you instead ran: git reset --hard A, that would tell Git to reset back to when A was the only thing committed, and B and C had never been committed. That is erasing history! But, Git is nice and doesn't throw away B and C just yet. They can still be recovered if you made a mistake. So that's why you shouldn't worry!
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)

🌐
Git
git-scm.com › book › en › v2 › Git-Basics-Undoing-Things
Git - Undoing Things
$ git checkout -- CONTRIBUTING.md $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: README.md -> README · You can see that the changes have been reverted.