9

I was messing about with reset after reading stuff in the Pro Git book.

I basically ended up doing a reset --hard to a revision 12 commits previous.

I can't seem to get back to the present, or the latest commit. I've tried reset using ORIG_HEAD and even feeding it in the sha1 of the revision to go forward to.

Running git status I get: Your branch is behind by 12 commits and can be fast-forwarded.

How do I move HEAD back to the latest commit?

3 Answers 3

13

Use the reflog to find out where you want to go. You can get it using git reflog and then just reset to the appropriate commit. Assuming you haven't done anything since you did the reset,

git reset --hard 'HEAD@{1}'

should do it.

8
  • Thank you, but it doesn't seem to help. The latest commit shown by reflog is the revision I'm currently on, it doesn't show the 12 missing ones :(
    – Gabe
    Commented Nov 21, 2011 at 14:46
  • What does the previous one show? reflog tracks the changes to the HEAD . Commented Nov 21, 2011 at 14:47
  • I ran git reset --hard 'HEAD@{1}' and now according to git status I'm now only 11 commits behind :)
    – Gabe
    Commented Nov 21, 2011 at 14:52
  • 1
    What do you get when you do a git reflog. You should get the list of commits and you can simply git reset to the right one. Commented Nov 21, 2011 at 14:56
  • 2
    No. When you do a git reflog, you will get a list of commits at which HEAD pointed to. HEAD@{n} will be the nth older commit. You will have the previous position of HEAD in the list of commits, git reset --hard to that. I can't really describe it more clearly than that. Commented Nov 21, 2011 at 15:06
4

It seems as though you've already pushed the 12 commits you reset. If that's the case, then

git merge --ff-only REMOTE/BRANCH_NAME

should work where REMOTE is the name of the remote (commonly origin) and BRANCH_NAME is the name of your current branch.

2
  • That did it, even without network access! Cool, though wish I really understood how it can merge with a remote branch even thought it can't connect?
    – Gabe
    Commented Nov 21, 2011 at 15:00
  • 1
    merges are local operations (it's the fetch that requires a network connection). Commented Nov 21, 2011 at 15:08
3

Another way (beside reflog) would be to use the fact that your branch seems to be referenced on the remotes namespace side, as a remote branch, which is why you see:

Your branch is behind by 12 commits and can be fast-forwarded.

A simple

git merge origin/yourBranch

should be enough to fast-forward the HEAD of your local branch back to where your remote branch was.

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