Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 ?

like image 760
e-satis Avatar asked Mar 29 '10 19:03

e-satis


People also ask

How do I get my last commit back?

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case.

Does git reset remove commit history?

The reset command actually will never delete a commit or change the content of a branch that you do not have currently checked out. It is just simply moving your HEAD pointer around. A very useful tool that is worth mentioning is the reflog.


2 Answers

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).

like image 71
VonC Avatar answered Sep 29 '22 13:09

VonC


Ok, found it.

You can use git reflog.

I didn't know what it was for, but now I can see it's a log of all the references HEAD have been pointing to.

like image 32
e-satis Avatar answered Sep 29 '22 14:09

e-satis