352

If one would checkout a branch:

git checkout 760ac7e

from e.g. b9ac70b, how can one go back to the last known head b9ac70b without knowing its SHA1?

8 Answers 8

549

If you remember which branch was checked out before (e.g. master) you could simply

git checkout master

to get out of detached HEAD state.

Generally speaking: git checkout <branchname> will get you out of that.

If you don't remember the last branch name, try

git checkout -

This also tries to check out your last checked out branch.

13
  • 64
    git checkout - - killer feature!
    – dimpiax
    Commented Jul 23, 2018 at 10:31
  • 1
    If you don't do a git checkout -b new_branch_name do you lose the commits made while in detached HEAD state?
    – jocassid
    Commented Dec 4, 2018 at 3:38
  • 2
    @jocassid Yes you do. They are present for a while but when git gc is run, they are removed forever. You can watch them with git reflog as long as they are still there.
    – eckes
    Commented Dec 4, 2018 at 5:03
  • won't you lose any commits/changes you made in detached HEAD if you do this? Isn't this a better way? stackoverflow.com/a/61489179/13087176
    – user13087176
    Commented Apr 28, 2020 at 20:13
  • @tipsyboopenstein correct. jocassid already mentioned this: stackoverflow.com/questions/11801071/…
    – eckes
    Commented Apr 28, 2020 at 20:21
22

Use git reflog to find the hashes of previously checked out commits.

A shortcut command to get to your last checked out branch (not sure if this work correctly with detached HEAD and intermediate commits though) is git checkout -

0
17

You may have made some new commits in the detached HEAD state. I believe if you do as other answers advise:

git checkout master
# or
git checkout -

then you may lose your commits!! Instead, you may want to do this:

# you are currently in detached HEAD state
git checkout -b commits-from-detached-head

and then merge commits-from-detached-head into whatever branch you want, so you don't lose the commits.

2
  • In some versions of git, git checkout master no longer works, but git checkout - does.
    – Mars
    Commented Jan 1, 2022 at 20:02
  • This solved my issue: # you are currently in detached HEAD state git checkout -b commits-from-detached-head Then I did git checkout <the branch you want to merge into> git merge commits-from-detached-head
    – gogobebe2
    Commented Dec 8, 2023 at 3:13
8

I had this edge case, where I checked out a previous version of the code in which my file directory structure was different:

git checkout 1.87.1                                    
warning: unable to unlink web/sites/default/default.settings.php: Permission denied
... other warnings ...
Note: checking out '1.87.1'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. 
Example:

  git checkout -b <new-branch-name>

HEAD is now at 50a7153d7... Merge branch 'hotfix/1.87.1'

In a case like this you may need to use --force (when you know that going back to the original branch and discarding changes is a safe thing to do).

git checkout master did not work:

$ git checkout master
error: The following untracked working tree files would be overwritten by checkout:
web/sites/default/default.settings.php
... other files ...

git checkout master --force (or git checkout master -f) worked:

git checkout master -f
Previous HEAD position was 50a7153d7... Merge branch 'hotfix/1.87.1'
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
2

git switch master to switch to the "master" branch.
git switch - to switch back to the previous branch.

1

In general: git checkout <branch*> (git checkout master is a special case).

*from which we (accidentally) detached a commit (using git checkout <commit_hash>)

0

Just in case anyone has the same edge case as me: I have a branch called test and was trying to make a branch called test/my-experimental-feature. That confused git because it thought I was referring to a branch that already exists. I changed it to test--my-experimental-feature and it worked fine.

-2

Since GitHub no longer uses master as the default branch for new repositories. git checkout master didn't work for me.

What worked was:
git checkout main

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