tags:

views:

80

answers:

2

i just checked out an earlier commit from my local git repo. I haven't made any changes to it, I was just looking at it. Now I want to go back to my latest commit - how do I do that? Thanks!


EDIT: exact command I used to check it out

git checkout e5dff6b3c5d704f9b598de46551355d18235ac08

Now when I type git log, at the top I see this checked out commit, but none of my later commits. Did I accidentally delete those?

+3  A: 

Try this first:

git checkout master

(If you're on a different branch than master, use the branch name there instead.)

If that doesn't work, try...

For a single file:

git checkout HEAD /path/to/file

For the entire repository working copy:

git reset --hard HEAD

And if that doesn't work, then you can look in the reflog to find your old head SHA and reset to that:

git reflog
git reset --hard <sha from reflog>

HEAD is a name that always points to the latest commit in your current branch.

Amber
but how do I view my commits to decide which SHA1 hash to give it?
yuval
You don't - you type `HEAD`, verbatim. Git already knows what `HEAD` means. However, if you really, really wanted to give it a SHA1 instead, you could use `git log` to look at the commit log.
Amber
when I run `git reset --hard HEAD` it brings me back to that checked out commit... I'll post the exact command I used to check it out.
yuval
(If you're curious, you can type `git rev-parse HEAD` and see that it gives you a SHA1 corresponding to your latest commit.)
Amber
I've added two new options above, try them (in order - try the checkout version first).
Amber
looks like they're not there with `git-log`. please see edit
yuval
From your edit, it looks like what you actually did was check out a full revision, which doesn't get rid of your branch - it actually treats it as a separate checkout, unrelated to your branch. `git checkout master` (or your branchname instead of `master`) should get you back to where you need to be.
Amber
hehe `git reflog` and `git reset --hard <sha from reflog>` worked PERFECTLY. thank you thank you thank you!
yuval
Amber: You might want to move `git checkout master` up to the top of the answer - since the OP checked out an SHA1 directly, HEAD was clearly detached, so that's probably the most helpful thing for others finding this question.
Jefromi
@Jefromi - yeah, probably a good idea.
Amber
+3  A: 

You probably want git checkout master, or git checkout [branchname].

wuputah