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
12601

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 Tower
git-tower.com › learn › git faq › how to undo, revert, or delete a git commit
How to Undo, Revert, or Delete a Git Commit | Learn Version Control with Git
When you want to undo your last local commit — one that hasn't been pushed to a remote yet — git reset is the right tool: ... This rewinds the branch pointer by one commit. The --soft flag preserves your changes as staged modifications, ...
Published   4 days ago
Discussions

version control - How do I undo the most recent local commits in Git? - Stack Overflow
This will not just 'cancel last commit', but will revert repo completely back to the previous commit. So you will LOOSE all changes committed in the last commit! 2017-03-21T12:09:47.413Z+00:00 ... You right, to undo this you can use git push -f HEAD@{1}: 2017-04-24T13:07:0... More on stackoverflow.com
🌐 stackoverflow.com
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
Undoing last commit
You should use git revert in this situation. It will undo the changes made by the faulty commit. Then push the newly created commit to remote. See https://git-scm.com/docs/git-revert for more info. More on reddit.com
🌐 r/git
12
6
January 7, 2021
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
Git - git-revert Documentation
Revert the changes done by commits from the fifth last commit in master (included) to the third last commit in master (included), but do not create any commit with the reverted changes. The revert only modifies the working tree and the index.
🌐
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 - 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.
Top answer
1 of 16
30139

Undo a commit & redo

$ git commit -m "Something terribly misguided" # (0: Your Accident)
$ git reset HEAD~                              # (1)
# === If you just want to undo the commit, stop here! ===
[ edit files as necessary ]                    # (2)
$ git add .                                    # (3)
$ git commit -c ORIG_HEAD                      # (4)
  1. git reset is the command responsible for the undo. It will undo your last commit while leaving your working tree (the state of your files on disk) untouched. You'll need to add them again before you can commit them again.
  2. Make corrections to working tree files.
  3. git add anything that you want to include in your new commit.
  4. Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEAD; commit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option.

Alternatively, to edit the previous commit (or just its commit message), commit --amend will add changes within the current index to the previous commit.

To remove (not revert) a commit that has been pushed to the server, rewriting history with git push origin main --force[-with-lease] is necessary. It's almost always a bad idea to use --force; prefer --force-with-lease instead, and as noted in the git manual:

You should understand the implications of rewriting history if you amend a commit that has already been published.


Further Reading

You can use git reflog to determine the SHA-1 for the commit to which you wish to revert. Once you have this value, use the sequence of commands as explained above.


HEAD~ is the same as HEAD~1. The article What is the HEAD in git? is helpful if you want to uncommit multiple commits.

2 of 16
13009

Undoing a commit is a little scary if you don't know how it works. But it's actually amazingly easy if you do understand. I'll show you the 4 different ways you can undo a commit.

Say you have this, where C is your HEAD and (F) is the state of your files.

   (F)
A-B-C
    ↑
  master

Option 1: git reset --hard

You want to destroy commit C and also throw away any uncommitted changes. You do this:

git reset --hard HEAD~1

The result is:

 (F)
A-B
  ↑
master

Now B is the HEAD. Because you used --hard, your files are reset to their state at commit B.

Option 2: git reset

Maybe commit C wasn't a disaster, but just a bit off. You want to undo the commit but keep your changes for a bit of editing before you do a better commit. Starting again from here, with C as your HEAD:

   (F)
A-B-C
    ↑
  master

Do this, leaving off the --hard:

git reset HEAD~1

In this case the result is:

   (F)
A-B-C
  ↑
master

In both cases, HEAD is just a pointer to the latest commit. When you do a git reset HEAD~1, you tell Git to move the HEAD pointer back one commit. But (unless you use --hard) you leave your files as they were. So now git status shows the changes you had checked into C. You haven't lost a thing!

Option 3: git reset --soft

For the lightest touch, you can even undo your commit but leave your files and your index:

git reset --soft HEAD~1

This not only leaves your files alone, it even leaves your index alone. When you do git status, you'll see that the same files are in the index as before. In fact, right after this command, you could do git commit and you'd be redoing the same commit you just had.

Option 4: you did git reset --hard and need to get that code back

One more thing: Suppose you destroy a commit as in the first example, but then discover you needed it after all? Tough luck, right?

Nope, there's still a way to get it back. Type this

git reflog

and you'll see a list of (partial) commit SHAs (that is, hashes) that you've moved around in. Find the commit you destroyed, and do this:

git checkout -b someNewBranchName shaYouDestroyed

You've now resurrected that commit. Commits don't actually get destroyed in Git for some 90 days, so you can usually go back and rescue one you didn't mean to get rid of.

🌐
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?

Find elsewhere
🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: <file> ... 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.
🌐
Hostman
hostman.com › tutorials › how to revert the last commit in git
Revert the Last Commit in Git - Step-by-Step Guide
December 26, 2025 - ... Once you save and exit the editor, Git will create a new commit to revert the changes. This is an output of git log: Another approach to revert the last commit is by using git reset --soft.
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland
🌐
GitKraken
gitkraken.com › home › learn › problems & solutions › how to revert a git commit
Git Revert Commit | Solutions to Git Problems
February 5, 2024 - If you need to make revisions to a commit that is not your last commit, the best solution is to create a new commit by reverting the old commit. As a Git best practice, you should avoid doing anything that will require you to force push — and rewrite the history — of your main branch(es).
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes › git revert
How to Revert a Commit in Git? | Atlassian Git Tutorial
Git revert expects a commit ref was passed in and will not execute without one. Here we have passed in the HEAD ref. This will revert the latest commit. This is the same behavior as if we reverted to commit 3602d8815dbfa78cd37cd4d189552764b5e96c58. Similar to a merge, a revert will create a new commit which will open up the configured system editor prompting for a new commit message.
🌐
DataCamp
datacamp.com › blog › git-undo-last-commit
Git Undo Last Commit: Step-by-Step Guide for Beginners | DataCamp
June 23, 2025 - Let’s say you made a few commits ... the main branch. Oops. Now you want to undo one of those commits without messing up the shared history. That’s where git revert saves the day. Run git revert HEAD~1. This creates a new commit that undoes the changes from the second-to-last commit (because ...
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-revert-to-last-commit
How to Revert to Last Commit? - Git
June 6, 2024 - Reverting to the last commit means discarding the changes made after the last commit and bringing your working directory back to the state it was in at that commit. This is useful when recent changes have introduced errors, and you need to return to a stable state. There are several ways to accomplish this in Git, each with its own use cases and benefits.
🌐
Reddit
reddit.com › r/git › undoing last commit
Undoing last commit : r/git
January 7, 2021 - You can revert as others have said, and I generally think that’s the best method. Doing so creates a new commit which exactly undoes all the changes in the previous commit. If creating a new commit is undesirable and you’re comfortable force pushing, you can use git reset --mixed HEAD~1 to move ...
🌐
DataCamp
datacamp.com › tutorial › git-revert-last-commit
Git Revert Last Commit: How to Safely Undo a Change in Git | DataCamp
July 8, 2025 - Learn how to git revert last commit with git revert HEAD to undo changes. See how it differs from git reset to handle merge conflicts safely in shared branches.
🌐
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 - You can see this history from a detached head with git log just like if you were on a branch. You don't have "future history" - all commits point in one direction, so you need to check out a later ref to get "future history". ... BE AWARE THAT YOU WILL LOSE YOUR CURRENT PROGRESS. ... It undoes those commits, but those changes in the commits are then available to work with. So say for example, I wanted to combine the last three commits, I can go:
🌐
KodeKloud
kodekloud.com › blog › git-uncommit-last-commit
How to Uncommit Last commit in Git (5 Scenarios)
November 25, 2025 - To see the history of your commits ... those changes with others or merge them to the main branch. You can use git revert <commit_hash> --no-edit to undo your commit and create a new commit that reverses the changes of the previous ...
🌐
PhoenixNAP
phoenixnap.com › home › kb › sysadmin › git revert commit: how to undo last commit
Git Revert Commit: How to Undo Last Commit
February 4, 2026 - For instance, you want to commit a couple of files in a single snapshot but forget to add one of the files before committing. This issue is easily solved using the --amend option. ... The --no-edit option amends the commit without changing the commit message. Note: If you need to restore a missing repository, refer to our guide How to Restore a Git Repository. ... You now know how to use the revert and reset commands to undo changes and revert to a previous commit.
🌐
Sentry
sentry.io › sentry answers › git › revert a git repository to a previous commit
Revert a Git repository to a previous commit | Sentry
To do this, you can use git reset --hard, specifying the commit to return to: ... This will return the repository’s files to their previous state and remove the most recent commit from the current branch’s history.
🌐
Warp
warp.dev › terminus by warp › git › undoing git commits
How To Undo Your Last Git Commit(s) | Warp
November 30, 2023 - If you decide to use git reset or git checkout to overwrite commits, then you can use git push --force to overwrite the remote branch with your local branch. This will overwrite any commits that were made after your last ...
🌐
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 - I have already initialized the ... looks like: ... To revert to the to the previous commit, run the git revert command along with the commit ID of the current commit....