How to go back to the last commit in the history after I used git reset to go to an older changeset?

e-satis picture e-satis · Mar 29, 2010 · Viewed 9.2k times · Source

Suppose my history goes that way :

A - B - C - D (master)

If I do git reset B, I'll got :

A - B (master)

Trouble is, git log now show me only the history from A to B, and I can't see C and D anymore.

How can I go back to D ?

Answer

VonC picture VonC · Mar 29, 2010

You should be able to see D with git reflog.

See this article for instance.

The only time commits are actually deleted is if you git gc --prune (so be careful with that one!).

If you run git reflog right now in a repository you’ve been working in, you’ll see lots of changes that look something like this:

c5c3a82... HEAD@{0}: pull origin featureB: Merge made by recursive.
49d0608... HEAD@{1}: reset --hard HEAD^: updating HEAD
3ed01b1... HEAD@{2}: pull origin featureA: Merge made by recursive.
49d0608... HEAD@{3}: pull origin bugfixJ: Merge made by recursive.
854d44e... HEAD@{4}: commit: Add more cowbell to foo.c
6dbc22d... HEAD@{5}: pull origin bugfixI: Merge made by recursive.
9bdb763... HEAD@{6}: commit: Remove weevils
8518f9d... HEAD@{7}: checkout: moving from wickedfeature to master

These lines can be broken down into 4 parts:

  • commit hash,
  • commit pointer,
  • action,
  • and extra info.

If we wanted to get back the commit that was lost at HEAD@{1}, we could just git reset --hard HEAD@{2}.
Now our current branch (and working copy) are set to the repository state before we did the reset.

If we wanted to just see what that state was, we could git checkout -b temp HEAD@{2} (or git checkout HEAD@{2} if you have git 1.5.0 and up).