If you want to commit on top of the current HEAD with the exact state at a different commit, undoing all the intermediate commits, then you can use reset to create the correct state of the index to make the commit.

# Reset the index and working tree to the desired tree
# Ensure you have no uncommitted changes that you want to keep
git reset --hard 56e05fced

# Move the branch pointer back to the previous HEAD
git reset --soft "HEAD@{1}"

git commit -m "Revert to 56e05fced"
Answer from Lara Bailey 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)

Discussions

How can I roll back to a previous version?
To checkout a previous commit in Git, you first need to find the commit hash. You can use the git log command to view the commit history. Once you have the commit hash, you can use the git checkout command to move to that commit. More on github.com
🌐 github.com
2
1
July 19, 2023
I want to revert the code to a previous commit.
git checkout -f HEAD~16 -- . will make your code go back 16 commits in your local storage, without rewriting any history with those 16 commits. The "changes" (reverting the past 16 commits) should be at the staging area at this moment. Now you can git commit to commit those reverted code and then git push to update your Github repo. To be sure you did it right, try git diff HEAD..HEAD~17 to compare your branch to the branch 17 commits ago (the 16 commits you wanted to revert + the new commit that did the revert = 17 commits). If the command outputs nothing, you're all good. More on reddit.com
🌐 r/git
4
2
November 8, 2022
Revert branch to earlier hash via PR
Reset hard to the old commit you want to return to. Reset soft to the tip of the branch. Commit, push, open PR More on reddit.com
🌐 r/git
8
1
February 24, 2025
How can I go back to an older commit in Git and make that be the new head (non destructively) without getting this "fast forward" error?
The options you list are your main options, with #2 probably preferred. Reverting a bunch of commits isn't tedious or error prone by the way, not sure why you think that. You could (if you wanted to get complicated) branch from your thursday commit and merge that branch back to your main branch, making sure to not to merge any changes but to throw away all the changes on your main branch. I would probably use git-commit-tree to facilitate that last part because i'm familiar with it, but i'm not sure it's the easiest or best way. More on reddit.com
🌐 r/git
8
7
November 12, 2019
🌐
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
The same reset technique works for returning to any earlier revision — just provide the commit hash instead of HEAD~1: ... If you only want to restore your whole project to a specific earlier state, read more about how to reset to a previous revision. What if you want to undo the effects of a commit that's not the most recent one — without touching any commits that came after it? That's what git revert is for.
Published   1 day ago
🌐
Codefinity
codefinity.com › courses › v2 › 7533d91f-0a23-44a3-afc7-c84d5072e189 › b9a4a4e8-3d95-4d5d-bf29-f87c3fd673a4 › c3bcd665-926a-44bf-adf1-d1f97167d536
Learn Reverting a Specific Commit | Undoing Changes
Basic syntax: ... Instead of using HEAD (which targets the latest commit), specify the hash of the commit (commit ID) you want to revert by replacing <commit-hash> with the actual hash value.
🌐
Codecademy
codecademy.com › article › git-reset-revert-previous-commits
How to Revert to Previous Commits Using Git Reset and Revert | Codecademy
The git revert command is another command that we can use to roll back to previous commits. This command produces a new commit that undoes the changes made in a specific commit, leaving the commit history intact.
Find elsewhere
🌐
Stack Abuse
stackabuse.com › git-revert-to-a-previous-commit
Git: Revert to a Previous Commit
February 16, 2023 - This means that by using just this command you'll not only revert to a previous commit, but you'll lose all working changes in the process. To avoid losing any working changes, you can use the stash and stash pop commands: $ git stash $ git reset --hard <hash-or-ref> $ git stash pop
🌐
PhoenixNAP
phoenixnap.com › home › kb › sysadmin › git revert commit: how to undo last commit
Git Revert Commit: How to Revert Previous Commit
February 4, 2026 - Note: Find out how to use Git checkout tags and why they are useful for your development project. Once a commit is uploaded to the server, it creates a more permanent project log. It is not advisable to use reset in this case as other developers may have retrieved the updated project already. Deleting updates from one system may cause conflicts with other team members. Instead, use the revert command: ... Make sure to enter the code for the hash you want to revert to.
🌐
Reddit
reddit.com › r/git › i want to revert the code to a previous commit.
r/git on Reddit: I want to revert the code to a previous commit.
November 8, 2022 -

I did some feature changes in my code but now I want it to be like the same code which was 16 commits ago. All my changes are pushed to GitHub and I do not want to lose my history or change my branch. How do I do this in the most minimum steps?

🌐
Reddit
reddit.com › r/git › revert branch to earlier hash via pr
r/git on Reddit: Revert branch to earlier hash via PR
February 24, 2025 -

We do work in a feature branch and merge it into our develop branch via PRs. There are about 30 commits that I need to back out of the develop branch, basically revert back to the last production build. In my first attempt I created a feature branch from the particular develop branch hash and then a PR was merged via the bitbucket web interface. This didn't work. Now I've reset the feature branch with git reset --hard commit-hash but bit bucket didn't detect any changes when trying to do a PR so I created a temp change and it picked that up but it still doesn't reverting back after a new PR was merged. What's the correct way to do this? Unfortunately we can' reset our push to develop directly.

🌐
GoLinuxCloud
golinuxcloud.com › home › devops › git › git revert to previous commit: reset, checkout, or rollback (examples)
Git Revert to Previous Commit: Reset, Checkout, or Rollback (Examples) | GoLinuxCloud
March 16, 2026 - You can revert to a previous commit using git revert <commit-hash> to safely undo changes, or git reset --hard <commit-hash> to move the branch pointer to that commit and remove later commits.
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-revert-a-commit-with-git-revert
How To Revert A Commit With Git Revert? - GeeksforGeeks
July 30, 2025 - It creates a new commit that reverses the changes introduced by a previous commit, maintaining a complete and accurate commit history. ... It adds a new commit that undoes the changes. You can revert individual or multiple commits. Ideal for shared branches because it doesn't rewrite history. In git, every commit is uniquely identifiable by its commit hash.
🌐
Medium
medium.com › @kensei.uchiha › reverting-a-commit-in-git-23fadaab6f8a
Reverting a Commit in Git. We have all been in a situation where… | by Kensei Uchiha | Medium
August 4, 2023 - Revert the commit: Once we have identified the commit that we want to revert, we can use the git revert command followed by the commit hash: git revert <commit-hash>This will create a new commit that undoes the changes made in the specified commit.
🌐
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 - If you want to completely discard the last five commits and reset your branch to the state of the previous commit, follow these steps. Run the following command to view the commit history: ... This will display a list of commits with their hashes. Identify the fifth last commit from the top. ... This will reset your branch to that specific commit, deleting all subsequent changes permanently. If your repository is remote (GitHub, GitLab, etc.), you’ll need to force push the changes:
🌐
TMS Outsource
tms-outsource.com › home › how to revert to a previous commit in github
How to Revert to a Previous Commit in GitHub - TMS Outsource
December 14, 2025 - To revert a commit, run git revert <commit-hash> which creates a new commit that undoes all changes from the specified commit while preserving the complete version history in your repository.
🌐
Codecademy
codecademy.com › docs › git › revert
Git | Revert | Codecademy
May 15, 2024 - This hash is a long string that is a mix of characters and numbers that is usually represented by a shorter version: a63b1329066c8ddd95c8d7bb201bacfb8b18e167 -> a63b132 · git revert can be used with the commit hash (as seen below) or with the ...
🌐
GeeksforGeeks
geeksforgeeks.org › git › reverting-a-file-to-previous-commit
Reverting A File To Previous Commit - Git
June 24, 2024 - In software development, you may encounter situations where you need to revert a file to its state from a previous commit. This could be due to a bug introduced in recent changes, a need to undo experimental modifications, or simply to restore a stable version. Git provides several methods to revert a file to its earlier state, offering flexibility depending on your needs. A commit in Git represents a snapshot of your project at a specific point in time. Each commit is identified by a unique SHA-1 hash, which you can use to reference it.
🌐
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:
🌐
Graphite
graphite.com › guides › git-revert-commit-after-pushing
git revert commit after pushing - Graphite
This is equivalent to git revert <commit-hash> where <commit-hash> is the hash of the most recent commit.