如何在 git 中有意分离 HEAD?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13292918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:43:56  来源:igfitidea点击:

How do I intentionally detach HEAD in git?

gitgit-checkout

提问by Russell Silva

If I do git checkout HEAD^, I get this:

如果我这样做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...
$

Veteran git users are probably very familiar with this. But if I do git checkout HEAD, nothing happens:

经验丰富的 git 用户可能对此非常熟悉。但如果我这样做git checkout HEAD,什么也不会发生:

$ git checkout HEAD
$

I'd like to create the "detached HEAD" state for the commit at the head of my current branch. How do I do that?

我想为当前分支头部的提交创建“分离的 HEAD”状态。我怎么做?

采纳答案by VonC

Since git 1.7.5 (April 2011), you can use the git checkout --detachcommand.

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

See commit 326696

提交 326696

checkout: introduce --detachsynonym for "git checkout foo^{commit}"

checkout: 介绍--detachgit 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 --detachon unborn branch, so this won't fail on a null HEAD:

Commit 8ced1aa(git 1.7.11.3, July 2012)--detach在未出生的分支上不允许,所以这不会在 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:

只有即将发布的 git 1.8.4.2 或 1.8.5(2013 年第 4 季度)澄清了语法。见提交 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 detach HEADat to clarify.

将这种情况分成两种句法形式,模仿描述部分如何显示这种用法的方式。
还要更新解释语法的文本以命名要分离的提交以HEAD进行澄清。

'git checkout' [--detach] <commit>::

Prepare to work on top of <commit>, by detaching HEADat it (see "DETACHED HEAD" section), and updating the index and the tree will be the state recorded in the commit plus the local modifications.

  1. When the <commit>argument is a branch name, the --detachoption can be used to detach HEADat the tip of the branch (git checkout <branch>would check out that branch without detaching HEAD).

  2. Omitting <branch>detaches HEADat the tip of the current branch.

准备在 之上工作<commit>,通过分离HEAD它(参见“DETACHED HEAD”部分),并更新索引和树将是提交中记录的状态加上本地修改。

  1. <commit>参数是分支名称时,该--detach选项可用于HEAD在分支的尖端分离(git checkout <branch>将在不分离的情况下检查该分支HEAD)。

  2. 省略在当前分支的尖端<branch>分离HEAD

That last point is precisely what you want to do for your current branch:

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

git checkout --detach

回答by platforms

This command creates a detached head state from any given branch name (in this case, master):

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

git checkout master^0

Checking out commit hashes also automatically creates a detached head state, no need for ^0:

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

git checkout 823112f444cb4aa70032feea6e8e5eb79d0e1ed0

And of course the shorter hashes as well:

当然还有更短的哈希值:

git checkout 823112f