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

Answer from Cascabel on Stack Overflow
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)

🌐
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 - Note that this will still leave your old branch there. It's great for testing alternative solutions. If you absolutely want to roll-back your branch to a previous commit, forgetting all newer commits: git reset --hard <oldcommitID>
Discussions

Revert commit on Website
This is not how you revert a git commit and not a good suggestion. This is how you reset the github repository to a previous version. More on github.com
🌐 github.com
8
18
Best way to revert the 3 last commit?
git reset HEAD^^^? More on reddit.com
🌐 r/git
6
4
October 19, 2017
Help: Rolled back instead of reverting and commits are gone from log. How do I recover?
check out git reflog. it should have the sha hash for the commit before you reverted. More on reddit.com
🌐 r/git
24
6
June 11, 2014
Git noob, have some questions, reverting to previous commits without harming branches? when is a branch worthwhile? more...
If you haven't heard of pro Git it's a really good official PDF / book on Git More on reddit.com
🌐 r/git
21
2
July 27, 2015
🌐
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.
🌐
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 - If you want to reset to the last commit and also remove all unstaged changes, you can use the --hard option: ... This will undo the latest commit, but also any uncommitted changes.
🌐
Medium
medium.com › @pinglinh › how-to-revert-to-a-previous-commit-when-youve-already-pushed-your-changes-910fec9af058
How to revert to a previous commit when you’ve already pushed your changes | by linhothy | Medium
September 25, 2017 - I needed to check the ID log of the commit I wanted to revert back to by using the following command: ... From this, I can see that the previous commit had the ID log of 080ebf7. ... 3. I then copied and pasted my code from the folder where I saved it at the beginning and did git status:
Find elsewhere
🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
If a file was changed in a commit, and you want to change it back to how it was in the previous commit, but keep the commit history, you can use git revert.
🌐
Sentry
sentry.io › sentry answers › git › revert a git repository to a previous commit
Revert a Git repository to a previous commit | Sentry
In Git, the word “revert” has a special meaning. You can use the git revert command to return your repository’s files to a previous state without rewriting the commit history. This is done by creating new commits that do the opposite of existing commits, i.e.
🌐
GitKraken
gitkraken.com › home › learn › problems & solutions › how to revert a git commit
Git Revert Commit | Solutions to Git Problems
February 5, 2024 - In this guide, we’ll delve into the practicalities of using the git revert command—a tool that developers frequently utilize to rectify past errors without losing work. Fundamentally, the git revert function produces an “equal but opposite” commit, effectively neutralizing the impact of a specific commit or group of commits.
🌐
Graphite
graphite.com › guides › revert-to-previous-commit-git
How to revert to a previous commit in Git
The git revert command generates a new commit that undoes the changes made in one or more existing commits. This is a safe way to undo changes, as it doesn't alter the project's commit history.
🌐
Joel Dare
joeldare.com › reverting-to-a-previous-git-commit
Reverting to a Previous Git Commit | Joel Dare
August 30, 2024 - How do I do it? I got back three answers, each of which look like they might work. My choices seem to be: ... The AI suggested git reset --hard c84bd but I didn’t think I actually want to reset --hard as I have other local files that are untracked and I want to keep those.
🌐
Medium
jeevabyte.medium.com › how-to-revert-to-a-previous-commit-in-git-and-make-it-the-latest-one-6ac766ff78f4
How to Revert to a Previous Commit in Git and Make It the Latest One | by Jeeva-AWSLabsJourney | Medium
March 9, 2025 - Whether you’re working on a project in VS Code or any Git-enabled environment, this guide will help you safely roll back your changes. Let’s say you’ve made five commits, but now you realize you need to go back to the fifth last commit and make it the latest one. Here’s how to do it properly. If you want to completely discard the last five commits and reset your branch to the state of the previous commit, follow these steps.
🌐
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).
🌐
GitHub
github.com › orgs › community › discussions › 60023
Revert commit on Website · community · Discussion #60023
You can also do git reset --hard HEAD~number, where number is how many commits you want to reset to (so HEAD~1 would revert to your last commit). After that, do push again as specified before.
🌐
Graphite
graphite.com › guides › git-revert-commit-after-pushing
git revert commit after pushing
git reset moves the branch pointer to a previous commit, effectively removing commits from the history. Use git revert for commits that have already been pushed to shared repositories, and git reset only for local commits that haven't been shared.
🌐
CloudBees
cloudbees.com › blog › git-revert-explained
Git Revert Explained: Safely Undoing Your Changes
November 29, 2021 - Reverting occurs in the Git CLI; you will need to use the git revert command, which looks like this. ... Using this command alone won’t do anything unless you specify the commit hash or reference (45111a).
🌐
GeeksforGeeks
geeksforgeeks.org › git › reverting-a-file-to-previous-commit
Reverting A File To Previous Commit - Git
June 24, 2024 - ... Find the commit hash using git log. ... Use git reset with the --hard option to move the file to the desired commit. ... After resetting, the file will be reverted in your working directory but not staged.
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes › git revert
How to Revert a Commit in Git? | Atlassian Git Tutorial
It's important to understand that git revert undoes a single commit—it does not "revert" back to the previous state of a project by removing all subsequent commits.
🌐
Linode
linode.com › docs › guides › revert-last-git-commit
How to Revert the Last Commit in Git | Linode Docs
February 15, 2024 - Use the --soft option to roll back to a previous commit, while preserving file changes in the working directory and staging area. ... Use the --hard option to likewise roll back to a previous commit.
🌐
Warp
warp.dev › terminus by warp › git › undoing git commits
How To Undo Your Last Git Commit(s) | Warp
November 30, 2023 - To resolve this issue, you will need to create a new branch and then resolve the merge conflicts when merging back into the main branch. git revert is the safest way of undoing commits as it does not overwrite the commit history.