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
🌐
Git
git-scm.com › docs › git-revert.html
Git - git-revert Documentation
Given one or more existing commits, revert the changes that the related patches introduce, and record some new commits that record them. 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).
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.

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
Reverting changes to a single file - why are so simple things so difficult?
It seems difficult at first, when you think of it in terms of "versioning files". But git is built around the idea of versioning the whole project, not individual files. This gives you a few options to revert a single file: 1. The file hasn't changed since the commit you want to revert for the file. In this case you can do git checkout COMMIT FILENAME and then commit the change. 2. The file has received further edits. You want the behavior of revert but only for one file. Easiest thing I can think of: git revert --no-commit COMMIT. Now all changes from that commit are reverted in the working tree and staged. git reset to unstage all the changes. git commit FILENAME... to commit only the reversial of specific files. git reset --hard to undo the remaining changes on the working tree. More on reddit.com
🌐 r/git
10
13
March 17, 2023
Revert commit on Website
Hello there! I'm beginner on GitHub, and can't resolve one problem - on desktop version i can "Revert changes in commit" in history without problems. But i can't find any way to revert commits on Website. I need to use the website and not the git console. A search on the web yielded no results ... More on github.com
🌐 github.com
8
18
How do you revert your code back to your last git commit?
Don't forget that you can always make a copy of the whole git repo folder on your computer so you can restore if you screw something up. Then you can mess around carefree! More on reddit.com
🌐 r/learnprogramming
21
1
June 4, 2022
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes › git revert
How to Revert a Commit in Git? | Atlassian Git Tutorial
The git revert command is used for undoing changes to a repository's commit history. Other 'undo' commands like, git checkout and git reset, move the HEAD and branch ref pointers to a specified commit. Git revert also takes a specified commit, however, git revert does not move ref pointers to this commit.
🌐
Git
git-scm.com › book › en › v2 › Git-Basics-Undoing-Things
Git - Undoing Things
$ git restore --staged CONTRIBUTING.md $ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) renamed: README.md -> README Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: CONTRIBUTING.md · The CONTRIBUTING.md file is modified but once again unstaged. What if you realize that you don’t want to keep your changes to the CONTRIBUTING.md file? How can you easily unmodify it — revert it back to what it looked like when you last committed (or initially cloned, or however you got it into your working directory)?
🌐
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.
🌐
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!
Find elsewhere
🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
It’s safer, because you can revert a revert. # Changed file git commit -am "bug introduced" git revert HEAD # New commit created reverting changes # Next, reapply the reverted commit git log # take hash from the revert commit git revert <rev commit hash> # reverted commit is back (new commit created again)
🌐
W3Schools
w3schools.com › git › git_revert.asp
Git Revert
The git revert command undoes a previous commit by creating a new commit that reverses the changes.
🌐
Reddit
reddit.com › r/git › reverting changes to a single file - why are so simple things so difficult?
r/git on Reddit: Reverting changes to a single file - why are so simple things so difficult?
March 17, 2023 -

I want to revert changes done in a commit to a single file, but not all the other changes which were done on this commit. It seems I can only find the option the revert all the changes done by this commit to all the files. (trying both with ToirtoiseGit and GitKraken)

🌐
Atlassian
atlassian.com › git › tutorials › undoing changes
Undoing Changes in Git | Atlassian Git Tutorial
December 15, 2025 - The preferred method of undoing shared history is git revert. A revert is safer than a reset because it will not remove any commits from a shared history. A revert will retain the commits you want to undo and create a new commit that inverts ...
🌐
Linux Kernel
kernel.org › pub › software › scm › git › docs › git-revert.html
git-revert(1)
June 20, 2025 - Given one or more existing commits, revert the changes that the related patches introduce, and record some new commits that record them. 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).
🌐
Microsoft Learn
learn.microsoft.com › en-us › azure › devops › repos › git › undo
Undo changes in your Git repo - Azure Repos | Microsoft Learn
When you want to undo changes in a Git repo, first decide what type of changes you want to undo. For example, you might want to: Discard uncommitted changes to a file by reverting the file to its last committed version.
🌐
GitHub
github.com › orgs › community › discussions › 60023
Revert commit on Website · community · Discussion #60023
To revert a commit you can git reset --hard<commit-hash> then git push origin <branch-name>, where would likely be main for you as I assume you don't know much about branching yet.
🌐
Reddit
reddit.com › r/learnprogramming › how do you revert your code back to your last git commit?
r/learnprogramming on Reddit: How do you revert your code back to your last git commit?
June 4, 2022 -

Sorry for the noob question but all the google answers are spread out over 10 years and totally different answers. I can't find a common consensus on this and don't want to screw anything up.

I just need to go back to my last commit. I committed and everything was fine but recent code changes broke the site and I need to go back to when things were working.

It says in the terminal:

(use "git restore <file>..." to discard changes in working directory)

This is what I'm looking for right? If I do "git restore <file>" it will discard all my changes and go back to what the file was like before I made the breaking changes?

Also, if so, I have a lot of files to change, if I just do "git restore" without a specified file will it restore ALL the files that have been modified so I don't have to go one by one?

🌐
Gitbybit
gitbybit.com › gitopedia › git-commands › git-revert
Gitopedia: git revert — create an undo commit
git revert creates a new commit that reverses the changes made by the specified commit, effectively canceling out its effects without altering the commit history.
🌐
GitProtect.io
gitprotect.io › strona główna › git undo: 13 ways to undo mistakes in git
Git Undo: 13 Ways to Undo Mistakes in Git - Blog | GitProtect.io
November 24, 2025 - Fortunately, there is a safe solution – the git revert command operation. In a public repository, ‘git revert’ is the safest way to undo changes because it preserves the commit history and avoids rewriting previous commits. This is especially important when a commit has already been pushed and is now public, as using ‘git revert’ prevents disrupting others’ workflows.
🌐
GitHub
github.blog › home › open source › how to undo (almost) anything with git
How to undo (almost) anything with Git - The GitHub Blog
July 23, 2024 - What’s happening: git revert will create a new commit that’s the opposite (or inverse) of the given SHA.
🌐
freeCodeCamp
freecodecamp.org › news › git-revert-file-reverting-a-file-to-a-previous-commit
Git Revert File – Reverting a File to a Previous Commit
November 7, 2024 - You will use the SHA hash to revert your file: 198d425 (HEAD -> main) initial c368a1c new removal bcbef35 updated readme 2 da9cc5f (origin/main) updated Readme a5150af first commit · So now that you know how to get the SHA code, you can use the git checkout command to revert your file to any commit you want by also passing the file name or file path:
🌐
Medium
medium.com › @marioekaputta721 › revert-your-work-with-git-revert-f3c5d66cf1f7
Revert Your Work with Git Revert. Git is software for tracking changes in… | by Mario Ekaputta | Medium
October 7, 2021 - So, that’s how I run my first git revert command. At this point, we should already know that git revert is some kind of ‘undo’ type command, however, it is not the same with traditional undo operations that we know.