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)

🌐
Git
git-scm.com › docs › git-revert.html
Git - git-revert Documentation
Continue the operation in progress using the information in .git/sequencer. Can be used to continue after resolving conflicts in a failed cherry-pick or revert. ... Skip the current commit and continue with the rest of the sequence. ... Forget about the current operation in progress. Can be used to clear the sequencer state after a failed cherry-pick or revert. ... Cancel the operation and return to the pre-sequence state. ... Revert the changes specified by the fourth last commit in HEAD and create a new commit with the reverted changes.
Discussions

How to Undo "git reset -HEAD~"?
Find the commit hash you want to reset to with git reflog, then do a git reset [hash]. On mobile so sorry for formatting More on reddit.com
🌐 r/Frontend
11
13
October 17, 2022
Git commands I run before reading any code
What Changes the Most · jj log --no-graph -r 'ancestors(trunk()) & committer_date(after:"1 year ago")' \ -T 'self.diff().files().map(|f| f.path() ++ "\n").join("")' \ | sort | uniq -c | sort -nr | head -20 Who Built This More on news.ycombinator.com
🌐 news.ycombinator.com
509
2338
April 14, 2026
🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
# 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)
🌐
Reddit
reddit.com › r/frontend › how to undo "git reset -head~"?
r/Frontend on Reddit: How to Undo "git reset -HEAD~"?
October 17, 2022 -

I was trying to revert my work to the last commit after experimenting with stuff but instead reverted it to the commit before that because I didn't know which git command I'm supposed to use. How do I move the current head forward? How do I get the work that's currently last commited on Github?

Been learning to code for over a year and I still have no idea how git works. Most stackflow results make no sense to me, tbh. I just can't ever wrap my head around how git works, other than initializing, commiting, and pushing. Thanks a lot in advance, any help is much appreciated.

🌐
CloudBees
cloudbees.com › blog › git-revert-explained
Git Revert Explained: Safely Undoing Your Changes
November 29, 2021 - We will also look at other command line options and how this relates to resetting, amending, or reverting multiple commits. You use git revert to record some new commits to reverse the effect of some earlier commits (possibly faulty ones). It is an “undo” command, but technically it is much more than that.
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes › git revert
How to Revert a Commit in Git? | Atlassian Git Tutorial
1$ git revert HEAD 2[main b9cd081] Revert "prepend content to demo file" 1 file changed, 1 deletion(-)
Find elsewhere
🌐
GitHub
gist.github.com › stevenyap › 7511407
Git reset and revert: Undoing changs · GitHub
git revert HEAD~3 # 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.
🌐
Git
git-scm.com › docs › git-reset
Git - git-reset Documentation
Reset the index and update the files in the working tree that are different between <commit> and HEAD, but keep those which are different between the index and working tree (i.e. which have changes which have not been added). Mainly exists to reset unmerged index entries, like those left behind by git am -3 or git switch -m in certain situations.
🌐
W3Schools
w3schools.com › git › git_revert.asp
Git Revert
Use git revert HEAD --no-edit to create a new commit that reverses the changes.
🌐
freeCodeCamp
freecodecamp.org › news › git-reset-hard-how-to-reset-to-head-in-git
Git Reset Hard – How to Reset to Head in Git
April 10, 2023 - So, when we are talking about resetting to HEAD, it means resetting the current branch to the most recent commit. Apart from the HEAD, you can also reset to other commits with the git reset --hard <commit-hash> command.
🌐
GitHub
docs.github.com › en › desktop › managing-commits › resetting-to-a-commit-in-github-desktop
Resetting to a commit in GitHub Desktop - GitHub Docs
For more information, see Committing and reviewing changes to your project in GitHub Desktop. You can reset to commit up to the most recent commit that has already been pushed to the remote repository. To undo a pushed commit without disrupting the commit history for other contributors, you can revert the commit.
🌐
GIGAZINE
gigazine.net › gsc_news › en › 20260410-git-command-before-reading-code
5 Git commands to help new project members quickly grasp the current state of the repository - GIGAZINE
April 10, 2026 - ◆ How often does it cause controversy? The following commands check how often the code has been rolled back or emergency patches have been applied. [code] git log --oneline --since='1 year ago' | grep -iE 'revert|hotfix|emergency|rollback' [code]
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-undo-a-commit-in-git
Undo a Commit in Git - GeeksforGeeks
2 weeks ago - Git provides multiple approaches to undo commits depending on whether changes should be preserved or already shared. Used to undo commits locally by moving the HEAD pointer to a previous commit, with options to keep or discard changes.
🌐
Hacker News
news.ycombinator.com › item
Git commands I run before reading any code | Hacker News
April 14, 2026 - What Changes the Most · jj log --no-graph -r 'ancestors(trunk()) & committer_date(after:"1 year ago")' \ -T 'self.diff().files().map(|f| f.path() ++ "\n").join("")' \ | sort | uniq -c | sort -nr | head -20 Who Built This
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-revert-a-commit-with-git-revert
How To Revert A Commit With Git Revert? - GeeksforGeeks
July 30, 2025 - When working with Git, it is common to make a commit that later needs to be undone whether due to a bug, accidental changes, or a mistake in the workflow. Instead of deleting or rewriting history, Git provides the git revert command to safely undo a specific commit by creating a new one that reverses its changes.
🌐
Nestify
nestify.io › blog › using-git-reset-to-revert-to-previous-commit
Using 'git reset' to revert to previous commit - Nestify
git reset –hard f414f31 git reset –soft HEAD@{1} git commit -m “Reverting to the state of the project at f414f31”
🌐
Codecademy
codecademy.com › article › git-reset-revert-previous-commits
How to Revert to Previous Commits Using Git Reset and Revert | Codecademy
Suppose we want to reset or move HEAD back to the second commit. In that case, we need to find the commit hash of Second commit, which is 95419f7. Now, let’s go back to the second commit using the git reset command:
🌐
Atlassian
atlassian.com › git › tutorials › resetting checking out and reverting
Resetting, Checking Out & Reverting | Atlassian Git Tutorial
December 16, 2025 - Note that this removes all of the subsequent changes to the file, whereas the git revert command undoes only the changes introduced by the specified commit. Like git reset, this is commonly used with HEAD as the commit reference. For instance, git checkout HEAD foo.py has the effect of discarding unstaged changes to foo.py.
Top answer
1 of 2
7

HEAD is where your workspace is currently in the tree of git commits; detached means that it doesn't correspond to a branch. To fix this, you should create a new branch with git checkout -b branch (replacing branch with the name you want to give your new branch).

If you want to drop the commits following the one you reset to, you can delete the master branch and re-create it:

git branch -D master
git checkout -b master

If you're working on a repository which is pushed elsewhere you'll need to do more work to fix things up, possibly forcing a push (and telling every one else to re-clone their workspace). If you have shared state, you should really create a revert commit; take a look at git revert (starting from master and reverting all the commits starting with the one following c70e611).

2 of 2
3

HEAD detached at c70e611

This is because when you did the git reset --hard, you were not on any branch at that time. You had a detached HEAD, and that detached head got moved with the git reset --hard command, along with a rewrite of your working tree to that state.

If you want some branch foo to be c70611, then:

git checkout foo
git reset --hard c70611

If this is considered to be a good state to push to foo's upstream then just git push <remote-name> foo.

There is a more direct way to force foo to c70611 without checking it out to be the current branch. Namely, you can rewrite what foo points to using the git update-ref command.

Before doing any of the above, I would pause and try to see how I had ended up in a detached state without noticing. Perhaps it was an unfinished rebase or whatever. Step one is to review the last few entries in the git reflog to help jog your memory.