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
Or will this git revert example leave four files in the local workspace and remove only the charlie.html file? If you chose the last outcome, you'd be correct. Here's why. 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.
🌐
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>"".
🌐
W3Schools
w3schools.com › git › git_revert.asp
Git Revert
Use git revert HEAD --no-edit to create a new commit that reverses the changes.
🌐
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.
🌐
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
🌐
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 - Git Revert targets an individual commit at an arbitrary point in the Git history and appends a new commit with the resulting inverse content. Git Reset can only work backward from the current commit. For example, if we want to undo old commits which are ten commits behind our current commit with Git Reset, we must remove all the commits history that occurred after the target commit in order to go back to undo that commit.
🌐
The Odin Project
theodinproject.com › lessons › foundations-git-basics
Git Basics | The Odin Project
There are two main reasons for doing this: first, if something you change turns out to cause some problems, it is easy to revert the specific change without losing other changes; and second, it enables you to write better commit messages. You’ll learn more about what a good commit message looks like in a future lesson! If you are using Visual Studio Code (and you should be if you’re following this curriculum), there’s a way to ensure that if you use git commit without the message flag (-m), you won’t get stuck writing your commit message in Vim.
🌐
Devart
devart.com › dbforge › sql › source-control › reverting-git-commit-with-examples.html
Reverting a Git Commit with Examples - Devart
For demo purposes, we have linked the BicycleStoreDemo database to the Git remote repository, BicycleStore-repository, and made several commits using dbForge Source Control for SQL Server. Afterwards, we are going to revert the commit by executing the git revert command from the command line.
🌐
Gitbybit
gitbybit.com › gitopedia › git-commands › git-revert
Gitopedia: git revert — create an undo commit
f4b5a62 (HEAD -> main) Revert "Fix typo in main code" a1b2c3d Update README b2c3d4e Fix typo in main code c3d4e5f Add new feature 2a291cc Initial commit · Now, the effects of the Fix typo in main code commit have been undone, but the commit itself is still part of the history, and a new commit has been created to record the reversal of its changes. ... GitWorking treeRepositoryStaging areaCommitCommit referenceCommit hashHEADTagBranchMergingMerge conflictRemote repositoryRemote-tracking branch
🌐
Reddit
reddit.com › r/git › 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?
r/git on Reddit: 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?
December 11, 2024 - So say for example, I wanted to combine the last three commits, I can go: ... This will undo those three commits, but keep the changes, I can then recommit them into a single one. Hope this helps, I know I use it all the time! ... Help: Rolled back instead of reverting and commits are gone from log. How do I recover? ... If need to go back to earlier git ...
🌐
CloudBees
cloudbees.com › blog › git-revert-explained
Git Revert Explained: Safely Undoing Your Changes
November 29, 2021 - 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. This process helps Git remove the unwanted commit from the codebase and retain the history of every commit and the reverted one.
🌐
Git
git-scm.com › cheat-sheet
Git Cheat Sheet
The entire Pro Git book written by Scott Chacon and Ben Straub is available to read online for free. Dead tree versions are available on Amazon.com · Every time we say <commit>, you can use any of these:
🌐
W3Schools
w3schools.com › git › git_reset.asp
Git Reset
git log --oneline e56ba1f (HEAD -> master) Revert "Just a regular update, definitely no accidents here..." 52418f7 Just a regular update, definitely no accidents here...
🌐
Rosettacommons
docs.rosettacommons.org › docs › latest › internal_documentation › Reverting-a-Commit
Reverting a Commit
Note that we'll also need the mainline version for the version prior to the commit we're reverting. 56494 - 199441208b2b2f2 56493 - 12a020b263aaafa 56492 - c4ba068409eef69 · (Note for git SHA1's you can shorten them as long as the shortened portion is unique in the repository.)
🌐
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!
🌐
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.
🌐
W3docs
w3docs.com › learn-git › git-revert.html
Git Revert - How To Use Git Revert | W3Docs Online Git Tutorial | W3Docs
#Initialized empty Git repository in /git_revert_example/.git/ touch w3docs_file git add w3docs_file git commit -m "original commit" #[master (root-commit) 299b15f] original commit #1 file changed, 0 insertions(+), 0 deletions(-) #create mode 100644 w3docs_file echo "original content" >> w3docs_file git commit -m "add new content to w3docs_file" #[master 3602d88] add new content to w3docs_file #1 file changed, 1 insertion(+) echo "prepended line content" >> w3docs_file git commit -m "prepend content to w3docs file" #[master 86bb32e] prepend content to w3docs file #1 file changed, 1 insertion(+) git log --oneline #86bb32e prepend content to w3docs file #3602d88 add new content to w3docs file #299b15f original commit
🌐
Better Stack
betterstack.com › community › questions › how-to-revert-multiple-commits
How Can I Revert Multiple Git Commits? | Better Stack Community
Revert Commits: Use git revert ... revert. You can specify multiple commits separated by spaces. For example, to revert commits abcdef1 and 0123456:...