0

I have a local git repository with only the main branch. So I did a few commits. Afterward, I decided to go back to the first commit. So I did

git checkout xxxxxx

But now when I do git log I can only see the first commit. How do I get back to the latest code?

5
  • stackoverflow.com/q/17995608/520162 could be quite relevant here.
    – eckes
    Commented Mar 2, 2015 at 14:57
  • git checkout master?
    – Mikolaj
    Commented Mar 2, 2015 at 14:57
  • Use this command carefully. The git checkout command deletes the unstaged and uncommitted changes of tracked files in the working tree and it is not possible to restore this deletion via Git.
    – gpullen
    Commented Mar 2, 2015 at 15:22
  • 1
    @gpullen: This is wrong. git checkout does not delete anything unstaged or overrides uncommitted changes. This is what git reset does. And yes, you have to be careful when doing a git reset --hard. But this is not the case for git checkout. checkout is safe!
    – eckes
    Commented Mar 2, 2015 at 15:44
  • Ok...so git checkout filename would replace with the latest version from the current branch and the changes will be discarded - no backup is kept
    – gpullen
    Commented Mar 2, 2015 at 16:07

1 Answer 1

1

To get back to the last branch you checked out:

git checkout -

Generally speaking:

git checkout <BRANCH-NAME> 

checks out that branch.

Not the answer you're looking for? Browse other questions tagged or ask your own question.