How do I revert a Git repository to a previous commit?
Solution 1
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. 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.
# This will create three separate revert commits:
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
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
Solution 2
Lots of complicated and dangerous answers here, but it's actually easy:
git revert --no-commit 0766c053..HEAD
git commit
This will revert everything from the HEAD back to the commit hash, meaning it will recreate that commit state in the working tree as if every commit after 0766c053
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.
Solution 3
Rogue Coder?
Working on your own and just want it to work? Follow these instructions below, they’ve worked reliably for me and many others for years.
Working with others? Git is complicated. Read the comments below this answer before you do something rash.
Reverting Working Copy to Most Recent Commit
To revert to the previous commit, ignoring any changes:
git reset --hard HEAD
where HEAD is the last commit in your current branch
Reverting The Working Copy to an Older Commit
To revert to a commit that's older than the most recent commit:
# Resets index to former commit; replace '56e05fced' with your commit code
git reset 56e05fced
# Moves pointer back to previous HEAD
git reset --soft HEAD@{1}
git commit -m "Revert to 56e05fced"
# Updates working copy to reflect the new commit
git reset --hard
Credits go to a similar Stack Overflow question, Revert to a commit by a SHA hash in Git?.
Solution 4
The best option for me and probably others is the Git reset option:
git reset --hard <commidId> && git clean -f
This has been the best option for me! It is simple, fast and effective!
** Note:** As mentioned in comments don't do this if you're sharing your branch with other people who have copies of the old commits
Also from the comments, if you wanted a less 'ballzy' method you could use
git clean -i
Solution 5
Before answering let's add some background, explaining what this HEAD
is.
First of all what is HEAD?
HEAD
is simply a reference to the current commit (latest) on the current branch. There can only be a single HEAD
at any given time (excluding git worktree
).
The content of HEAD
is stored inside .git/HEAD
, and it contains the 40-bytes SHA-1 hash of the current commit.
detached HEAD
If you are not on the latest commit - meaning that HEAD
is pointing to a prior commit in history it's called detached HEAD
.
On the command-line it will look like this - SHA-1 hash instead of the branch name since the HEAD
is not pointing to the the tip of the current branch:
A few options on how to recover from a detached HEAD:
git checkout
git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits t go back
This will checkout new branch pointing to the desired commit. This command will checkout to a given commit.
At this point you can create a branch and start to work from this point on:
# Checkout a given commit.
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
# in order to be able to update the code.
git checkout <commit-id>
# Create a new branch forked to the given commit
git checkout -b <branch name>
git reflog
You can always use the reflog
as well. git reflog
will display any change which updated the HEAD
and checking out the desired reflog entry will set the HEAD
back to this commit.
Every time the HEAD is modified there will be a new entry in the reflog
git reflog
git checkout HEAD@{...}
This will get you back to your desired commit.
git reset HEAD --hard <commit_id>
"Move" your HEAD
back to the desired commit.
# 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.
- Note: (Since Git 2.7) you can also use the
git rebase --no-autostash
as well.
This schema illustrates which command does what. As you can see there reset && checkout
modify the HEAD
.
Crazy Serb
Updated on July 11, 2022Comments
-
Crazy Serb almost 2 years
How do I revert from my current state to a snapshot made on a certain commit?
If I do
git log
, then I get the following output:$ git log commit a867b4af366350be2e7c21b8de9cc6504678a61b` Author: Me <[email protected]> Date: Thu Nov 4 18:59:41 2010 -0400 blah blah blah... commit 25eee4caef46ae64aa08e8ab3f988bc917ee1ce4 Author: Me <[email protected]> Date: Thu Nov 4 05:13:39 2010 -0400 more blah blah blah... commit 0766c053c0ea2035e90f504928f8df3c9363b8bd Author: Me <[email protected]> Date: Thu Nov 4 00:55:06 2010 -0400 And yet more blah blah... commit 0d1d7fc32e5a947fbd92ee598033d85bfc445a50 Author: Me <[email protected]> Date: Wed Nov 3 23:56:08 2010 -0400 Yep, more blah blah.
How do I revert to the commit from November 3, i.e. commit
0d1d7fc
?-
Admin almost 10 yearsRelated How to undo the last Git commit?.
-
Aurelio almost 9 yearsHere's a very clear and thorough post about undoing things in git, straight from Github.
-
The Muffin Man over 6 yearsI love git, but the fact that there's 35 answers to something that should be incredibly simple exposes a huge issue with git. Or is it the docs?
-
-
New Alexandria over 11 years@Rod's comment on
git revert HEAD~3
as the best wat to revert back3
commits is am important convention. -
Lennon over 11 yearsI did that, but then I wasn't able to commit and push to the remote repository. I want a specific older commit to become HEAD...
-
Spoeken over 11 yearsCould you write the whole number? like:
git reset --hard 0d1d7fc32e5a947fbd92ee598033d85bfc445a50
-
Cascabel over 11 years@MathiasMadsenStav Yes, you can of course specify commits by the full SHA1. I used abbreviated hashes to make the answer more readable, and you also tend to use them if you're typing out. If you're copying and pasting, by all means use the full hash. See Specifying Revisions in man git rev-parse for a full description of how you can name commits.
-
Mirko Akov over 10 yearsYou can use
git revert --no-commit hash1 hash2 ...
and after this just commit every single revert in one commitgit commit -m "Message"
-
Admin almost 10 yearsObligatory Warning: don't do this if you're sharing your branch with other people who have copies of the old commits, because using a hard reset like this will force them to have to resynchronize their work with the newly reset branch. For a solution that explains in detail how to safely revert commits without losing work with a hard reset, see this answer.
-
Admin almost 10 yearsIf you really do want to have individual commits (instead of reverting everything with one big commit), then you can pass
--no-edit
instead of--no-commit
, so that you don't have to edit a commit message for each reversion. -
timhc22 over 9 yearsIf one of the commits between 0766c053..HEAD is a merge then there will be an error popping up (to do with no -m specified). This may help those encountering that: stackoverflow.com/questions/5970889/…
-
Alex G almost 8 years
$ git revert --no-commit 53742ae..HEAD
returnsfatal: empty commit set passed
-
andreyro over 7 yearsNOTE: New files not added in index are not stashed. You have too add them or manually delete them.
-
Romain Valeri about 5 yearsWhy oh why clearing stash? In addition to being a non-solution, this is actually harmful. Reading the very first sentence of the question immediately invalidates the stash solution (which could be useful ONLY to reset to the LAST commit).
-
Henke over 3 yearsI think this is an answer to a rather different question - stackoverflow.com/q/1125968. I interpret the question here to concern the remote repository.
-
wensiso over 2 yearsIf you remove that '..HEAD' at the end of the command, you can remove only a specific commit. For example:
git revert --no-commit 0766c053
will remove only the specific changes made for 0766c053 keeping all changes after 0766c053 untouched. -
entropo over 2 years@Lennon. Say you made a change, committed, and pushed it. You want both local and remote to appear as though this never happened. First
git reset --hard HEAD^
You've now blown away all local changes from the last commit. Then:git push --force origin HEAD
This takes the current HEAD commit in local and overwrites the HEAD in the remote, removing the last commit. Note: This isn't a secure way to delete secrets accidentally pushed to a remote. Assume all secrets are compromised then See caveats for '--force': evilmartians.com/chronicles/… -
entropo over 2 yearsNote: This only applies to your local repository. If you've pushed commits to a remote, just a reset locally will not change anything on the remotes. It is more complicated and dangerous and full of caveats to try to undo changes on a remote.
-
Suvro Choudhury over 2 years@cascabel git revert -m <number> <full SHA1> worked fine for me! I prefer this over git revert <commit SHA1>
-
Asking over 2 years@Cascabel, could you help with this stackoverflow.com/questions/69985427/…
-
James L. over 2 yearsAfter reseting you need to do
git push -f
. Be VERY careful because it will erase the commits in front of the one you reset to from your remote branch! -
Pedro A over 2 yearsAs @timhc22 mentioned, this doesn't work if there is one or more merge commits in between (which can happen frequently). An answer that really works in all cases and is just as safe is here: stackoverflow.com/a/15563149/4135063
-
agent18 over 2 yearsgit revert is not all that innocent. It has it's issues as well: stackoverflow.com/questions/44266207/…
-
David Callanan over 2 yearsNote that if you want to revert to the previous commit without the commit ID you can use
git revert --no-commit HEAD~1..HEAD
and if you want to revert the last 5 commits you can dogit revert --no-commit HEAD~5..HEAD
-
kat over 2 yearsIt's worth noting this will permanently delete any uncommitted files in your source directory :/
-
John Smith Optional about 2 yearsthat is the answer
-
Cesar about 2 yearsthis is the only answer here that actually answers the question without resorting to force pushes
-
the Hutt about 2 years
--soft
doesn't loose the changes. That's very important!! -
BabyishTank almost 2 yearsThis move all the changes back into the stage area, and you still have to do extra work to get rid of them
-
Christian Vincenzo Traina almost 2 yearsWhat is the
1
in-m
?