我如何故意将HEAD分离为git?

问题描述 投票:35回答:2

如果我做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 git-checkout
2个回答
33
投票

从git 1。7。5(2011年4月)开始,您可以使用git checkout --detach命令。

commit 326696

checkout:介绍--detachgit 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>之上工作,通过分离HEADsee "DETACHED HEAD" section),并更新索引和树将是提交中记录的状态加上本地修改。

  1. <commit>参数是分支名称时,--detach选项可用于在分支的尖端分离HEADgit checkout <branch>将检查该分支而不分离HEAD)。
  2. 省略<branch>HEAD分离到当前分支的顶端。

最后一点正是您想要为当前分支做的事情:

git checkout --detach

10
投票

此命令从任何给定的分支名称(在本例中为master)创建一个分离的头状态:

git checkout master^0

检出提交哈希还会自动创建一个分离的头状态,不需要^0

git checkout 823112f444cb4aa70032feea6e8e5eb79d0e1ed0

当然还有较短的哈希值:

git checkout 823112f
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.