Reputation: 96541
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?
Upvotes: 365
Views: 223360
Reputation: 956
git switch master
to switch to the "master" branch.
git switch -
to switch back to the previous branch.
Upvotes: 2
Reputation: 1
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
Upvotes: -3
Reputation: 6881
In general: git checkout <branch*>
(git checkout master
is a special case).
*from which we (accidentally) detached a commit (using git checkout <commit_hash>
)
Upvotes: 1
Reputation: 1598
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.
Upvotes: 0
Reputation:
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.
Upvotes: 18
Reputation: 587
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'.
Upvotes: 8
Reputation: 67177
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.
Upvotes: 565
Reputation: 265928
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 -
Upvotes: 24