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?

Matt
  • 74,352
  • 26
  • 153
  • 180
Gabe
  • 1,078
  • 1
  • 11
  • 17

3 Answers3

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.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • 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 Nov 21 '11 at 14:46
  • What does the previous one show? reflog tracks the changes to the HEAD . – Noufal Ibrahim Nov 21 '11 at 14:47
  • I ran `git reset --hard 'HEAD@{1}'` and now according to `git status` I'm now only 11 commits behind :) – Gabe Nov 21 '11 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. – Noufal Ibrahim Nov 21 '11 at 14:56
  • HEAD@{0} Shows me --hard resetting to historical commit. All the way down to HEAD@{17} which shows me cloning a branch (the beginning). So the 11/12 commits are not listed. – Gabe Nov 21 '11 at 15:04
  • 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 `n`th 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. – Noufal Ibrahim Nov 21 '11 at 15:06
  • Of course you are right and I'm being a numpty. New to Stackoverflow so I'll have a fiddle and try to give you lots of rep :) If you have a min would you kindly have a quick look at my other [question](http://stackoverflow.com/questions/8213926/git-simplest-way-of-squashing-commits-on-master)? Thanks a lot :) – Gabe Nov 21 '11 at 15:20
  • This is one of these answers I wish I could upvote twice - thank you. – Ben Jan 14 '15 at 16:48
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.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
  • 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 Nov 21 '11 at 15:00
  • 1
    `merge`s are local operations (it's the `fetch` that requires a network connection). – Noufal Ibrahim Nov 21 '11 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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250