0

Suppose I'm working on master and I've commit 5 changesets:

v1 -- v2 -- v3 -- v4 -- v5

now I do 'git reset --hard v3' to go back to a particular point. At this stage, 'git log' will only show the 1st 3 commits and the hash for v4 and v5 will not be displayed. How can I get back to v5 easily?

(I did find a way to do this by poking into the .git directory but it's tedious and I'd like to avoid dealing with .git directory directly).

dcastro
  • 63,281
  • 21
  • 137
  • 151
lang2
  • 10,393
  • 16
  • 76
  • 123

2 Answers2

2

The git reflog should contain an entry about the former HEAD. You can easily git checkout that state (HEAD@{N}) and work with it.

TimWolla
  • 30,523
  • 8
  • 64
  • 89
1

For this you can use the git reflog command. git reflog shows you the old positions of HEAD with the corresponding SHA-1 keys and the operation done.

You should be able to easily find the SHA-1 key you are looking for. And the you can just use git branch -f <SHA-1 Key> or git reset --hard <SHA-1 Key> etc..

Here is there documentation, if you want to know more.

Hope that helps!

EDIT: And as TimWolla states, you can also use HEAD@{N} to navigate through the old HEAD references.

Sascha Wolf
  • 17,034
  • 4
  • 47
  • 72