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 <me@me.com>
Date: Thu Nov 4 18:59:41 2010 -0400
blah blah blah...
commit 25eee4caef46ae64aa08e8ab3f988bc917ee1ce4
Author: Me <me@me.com>
Date: Thu Nov 4 05:13:39 2010 -0400
more blah blah blah...
commit 0766c053c0ea2035e90f504928f8df3c9363b8bd
Author: Me <me@me.com>
Date: Thu Nov 4 00:55:06 2010 -0400
And yet more blah blah...
commit 0d1d7fc32e5a947fbd92ee598033d85bfc445a50
Author: Me <me@me.com>
Date: Wed Nov 3 23:56:08 2010 -0400
Yep, more blah blah.
How do revert to the commit from November 3, i.e. commit 0d1d7fc
?
As your commits are pushed remotely, you need to remove them. Let me assume your branch is develop and it is pushed over origin.
You first need to remove develop from origin:
Then you need to get develop to the status you want, let me assume the commit hash is EFGHIJK:
Lastly, push develop again:
The best option for me and probably others is the Git reset option:
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
You can complete all these initial steps yourself and push back to git repo.
Pull the latest version of your repository from Bitbucket using the
git pull --all
command.Run the git log command with -n 4 from your terminal. The number after the -n determines the number of commits in the log starting from the most recent commit in your local history.
$ git log -n 4
Reset the head of your repository's history using the
git reset --hard HEAD~N
where N is the number of commits you want to take the head back. In the following example the head would be set back one commit, to the last commit in the repository history:Push the change to git repo using
git push --force
to force push the change.If you want git repository to a previous commit
If the situation is an urgent one, and you just want to do what the questioner asked in a quick and dirty way, assuming your project is under directory "my project":
Copy the whole directory and call it something else, like "my project - copy"
Do:
You then have two versions on your system... you can examine or copy or modify files of interest, or whatever, from the previous commit. You can completely discard the files under "my project - copy", if you have decided the new work was going nowhere...
The obvious thing if you want to carry on with the state of the project without actually discarding the work since this retrieved commit is to rename your directory again: Delete the project containing the retrieved commit (or give it a temporary name) and rename your "my project - copy" directory back to "my project". Then probably do another commit fairly soon.
Git is a brilliant creation but you can't just "pick it up on the fly": also people who try to explain it far too often assume prior knowledge of other VCS [Version Control Systems] and delve far too deep far too soon, and commit other crimes, like using interchangeable terms for "checking out" - in ways which sometimes appear almost calculated to confuse a beginner.
To save yourself much stress you have to pretty much have to read a book on Git - I'd recommend "Version Control with Git". And if you can trust me (or rather my scars) when I say "have to", it follows that you might as well do it NOW. Much of the complexity of Git comes from branching and then remerging. But from your question there's no reason why people should be blinding you with science.
Especially if, for example, this is a desperate situation and you're a newbie with Git!
PS: One other thought: It is (now) actually quite simple to keep the Git repository ("repo") in a directory other than the one with the working files. This would mean you would not have to copy the entire Git repository using the above quick & dirty solution. See the answer by Fryer using --separate-git-dir here. Be warned, though: If you have a "separate-directory" repository which you don't copy, and you do a hard reset, all versions subsequent to the reset commit will be lost forever, unless you have, as you absolutely should, regularly backed up your repository, preferably to the cloud (e.g. Google Drive) among other places.
On GitKraken you can do this:
Obs.: You need to be care because all the commit history after the hard reset are lost and this action is irreversible. You need to be sure what you doing.
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:
Or if you want to make commits while you're there, go ahead and make a new branch while you're at it:
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:
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.
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 to move HEAD back to a previous location? (Detached head)