38

I did a git reset HEAD~1 to go back one commit. I did this a number of times.

I now want to go back to where HEAD was originally but am not sure how to move my HEAD forward.

Does anyone know what command I need to use?

1-2-3-4-5-6

Originally I was at 6 and I reset back to 3. I now want to go back to 5. My understanding is that since I did not do git reset --hard my original files from commit 6 is still available. Theoretically I should be able to un-reset and move back up correct?

2
  • If it's the latest commit on a branch (e.g. master), you can just do git checkout master. If it's not, you can checkout the specific SHA1, or checkout the tip of the branch and work backwards. There's probably other ways as well. Commented Jan 14, 2014 at 3:02
  • If you didn't do a hard reset, your changes are not discarded. However, the commits themselves have been removed from the index. The changes are just in the files in your working directory. Using git reflog will let you fix this. Commented Jan 14, 2014 at 3:09

1 Answer 1

49

use git reflog to see SHA-1 of last operations and then do git reset --hard <sha1>.

Git keeps objects (and their SHA-1 respectively) even they go "out of scope" until next git gc invocation. So if you think, you've lost something in the project history, use git reflog to see if that smth is there.

2
  • 2
    More precisely, it keeps them until the reflog entry expires and a git gc occurs (gc is invoked automatically when needed). The reflog is the easy way to find them so "reflog expiry" is the safer way to think about how long things are recoverable. By default, the expiration is at least 30 days, and for some entries, 90 days (the details are beyond the scope of this comment :-) ).
    – torek
    Commented Jan 14, 2014 at 3:21
  • I'd suggest to post a separate question clearly explaining what error you receive and on which step. Commented Sep 30, 2014 at 6:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.