如果我做git checkout HEAD^
,我得到这个:
$ git checkout HEAD^
Note: checking out 'HEAD^'.
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...
$
经验丰富的git用户可能对此非常熟悉。但如果我做git checkout HEAD
,没有任何反应:
$ git checkout HEAD
$
我想在我当前分支的头部为提交创建“分离的HEAD”状态。我怎么做?
从git 1。7。5(2011年4月)开始,您可以使用git checkout --detach
命令。
checkout
:介绍--detach
“git checkout foo^{commit}
”的同义词例如,在进行临时合并时可以使用此方法来测试两个主题是否可以很好地协同工作。
Commit 8ced1aa(git 1。7。11。3,2012年7月)不允许在未出生的分支上使用--detach
,所以这不会在null HEAD
上失败:
git checkout --orphan foo
git checkout --detach
git symbolic-ref HEAD
只有即将推出的git 1.8.4.2或1。8。5(2013年第4季度)才能澄清语法。见commit 26776c9:
将此案例分为两种语法形式,模仿描述部分显示此用法的方式。 还要更新解释语法的文本,以便将提交的名称分离为
HEAD
以澄清。
'git checkout' [--detach] <commit>::
准备在
<commit>
之上工作,通过分离HEAD
(see "DETACHED HEAD" section),并更新索引和树将是提交中记录的状态加上本地修改。
- 当
<commit>
参数是分支名称时,--detach
选项可用于在分支的尖端分离HEAD
(git checkout <branch>
将检查该分支而不分离HEAD
)。- 省略
<branch>
将HEAD
分离到当前分支的顶端。
最后一点正是您想要为当前分支做的事情:
git checkout --detach
此命令从任何给定的分支名称(在本例中为master)创建一个分离的头状态:
git checkout master^0
检出提交哈希还会自动创建一个分离的头状态,不需要^0
:
git checkout 823112f444cb4aa70032feea6e8e5eb79d0e1ed0
当然还有较短的哈希值:
git checkout 823112f