2

I commited my code, but did not push it. Let's call this commit #5 Later I needed to try an earlier version. So I checkout commit #3. Now I want to get back to my commit #5. How do I find out its commit id, and return to it?

Tried -git log, but it only shows commits from #3 to #1.

The Cook
  • 1,403
  • 3
  • 17
  • 33
  • did you try `git checkout HEAD` (I am guessing... I do not know if this actually work) – Riccardo Petraglia Nov 01 '16 at 13:59
  • Possible duplicate of [How can I recover a lost commit in Git?](http://stackoverflow.com/questions/10099258/how-can-i-recover-a-lost-commit-in-git) – Thibault D. Nov 01 '16 at 14:38
  • (also called _dangling_ commits because not branch points to them but they're still around somewhere) – Thibault D. Nov 01 '16 at 14:38
  • 1
    Do you see your commit #5 with this command: log --graph --decorate --oneline --all ? – Ivan Nov 01 '16 at 15:12
  • 2
    `git log` shows commits starting from HEAD, i.e., from your *current commit*. If you meant that you used `git checkout` to set HEAD to an earlier commit, then that would also explain the `git log` output. Simply `git checkout` some other commit, or a branch *name* such as `master`, to move your HEAD elsewhere, and then `git log` will show commits working backwards from there. – torek Nov 01 '16 at 17:47

1 Answers1

1

You can use this method to make HEAD re-point to commit #5, just run this command:

git checkout branch_name

After that, refer to lvan's comment to run this command to the see log:

git log --oneline --decorate --graph --all

You will find it will move the head to the latest commit #5

lake
  • 112
  • 4
  • To complete the above: after you run git log --online..., and you see your "lost" commit you can simply git reset --hard to it – GM1 May 25 '20 at 10:28