163

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?

The 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?

Brad Koch
  • 17,848
  • 18
  • 106
  • 133
Yuval Karmi
  • 25,609
  • 38
  • 119
  • 173

4 Answers4

219

Try this first:

git checkout master

(If you're on a different branch than master (or main), 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.

Conrad
  • 2,129
  • 28
  • 49
Amber
  • 477,764
  • 81
  • 611
  • 541
  • but how do I view my commits to decide which SHA1 hash to give it? – Yuval Karmi Aug 30 '10 at 15:43
  • 1
    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 Aug 30 '10 at 15:44
  • 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 Karmi Aug 30 '10 at 15:45
  • (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 Aug 30 '10 at 15:45
  • I've added two new options above, try them (in order - try the checkout version first). – Amber Aug 30 '10 at 15:47
  • 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 Aug 30 '10 at 15:49
  • hehe `git reflog` and `git reset --hard ` worked PERFECTLY. thank you thank you thank you! – Yuval Karmi Aug 30 '10 at 15:49
  • 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. – Cascabel Aug 30 '10 at 16:02
80

To undo git checkout do git checkout -, similarly to cd and cd - in shell.

18

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

wuputah
  • 11,175
  • 1
  • 42
  • 60
3

If you are using github, chances are the name for default branch is no longer master. Try git checkout main instead.

Ng Ju Ping
  • 157
  • 1
  • 2
  • 1
    Come on! This question was ASKED 10 years, 9 months ago (at the time I am writing this comment). The default branch was `master` at that time. And the question is not related to checking out to a non-existing branch. :D – Hamees A. Khan Jun 07 '21 at 10:59