How do I intentionally detach HEAD in git?
This command creates a detached head state from any given branch name (in this case, master):
git checkout master^0
Checking out commit hashes also automatically creates a detached head state, no need for ^0
:
git checkout 823112f444cb4aa70032feea6e8e5eb79d0e1ed0
And of course the shorter hashes as well:
git checkout 823112f
Since git 1.7.5 (April 2011), you can use the git checkout --detach
command.
(Since Git 2.23 (Q3 2019), you would use git switch --detach
)
See commit 326696
checkout
: introduce --detach
synonym for "git checkout foo^{commit}
"
For example, one might use this when making a temporary merge to test that two topics work well together.
Commit 8ced1aa (git 1.7.11.3, July 2012) disallows --detach
on unborn branch, so this won't fail on a null HEAD
:
git checkout --orphan foo
git checkout --detach
git symbolic-ref HEAD
Only the upcoming git 1.8.4.2 or 1.8.5 (Q4 2013) clarifies the syntax. See commit 26776c9:
Separate this case into two syntactical forms, mimicking the way how the DESCRIPTION section shows this usage.
Also update the text that explains the syntax to name the commit to detachHEAD
at to clarify.
'git checkout' [--detach] <commit>::
Prepare to work on top of
<commit>
, by detachingHEAD
at it (see "DETACHED HEAD" section), and updating the index and the tree will be the state recorded in the commit plus the local modifications.
When the
<commit>
argument is a branch name, the--detach
option can be used to detachHEAD
at the tip of the branch (git checkout <branch>
would check out that branch without detachingHEAD
).Omitting
<branch>
detachesHEAD
at the tip of the current branch.
That last point is precisely what you want to do for your current branch:
git checkout --detach