签出 Git 标签会导致“分离的 HEAD 状态”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5582208/
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
Checking out Git tag leads to "detached HEAD state"
提问by Khriz
I'm developing a deployment script for my git project and I just started using tags. I've added a new tag called v2.0
:
我正在为我的 git 项目开发部署脚本,我刚刚开始使用标签。我添加了一个名为 的新标签v2.0
:
git tag -a v2.0 -m "Launching version 2.0"
And I've pushed this tag to the remote repository
我已将此标签推送到远程存储库
git push --tags
When I try to execute the deployment script and check out the v2.0
tag I get this message:
当我尝试执行部署脚本并检查v2.0
标签时,我收到以下消息:
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
您处于“分离头”状态。您可以环顾四周,进行实验性更改并提交它们,并且您可以通过执行另一次检出来放弃您在此状态下所做的任何提交,而不会影响任何分支。如果你想创建一个新分支来保留你创建的提交,你可以(现在或以后)通过再次使用 -b 和 checkout 命令来这样做。示例: git checkout -b new_branch_name HEAD 现在位于
Is that normal? The repository is in limbo because if I do:
这是正常的吗?存储库处于不确定状态,因为如果我这样做:
git branch
I get this output:
我得到这个输出:
* (no branch)
master
Sorry if this is obvious but I couldn't figure it out.
对不起,如果这很明显,但我无法弄清楚。
回答by Noufal Ibrahim
Okay, first a few terms slightly oversimplified.
好的,首先有几个术语稍微过于简化了。
In git
, a tag
(like many other things) is what's called a treeish. It's a way of referring to a point in in the history of the project. Treeishes can be a tag, a commit, a date specifier, an ordinal specifier or many other things.
在 中git
, a tag
(像许多其他事物一样)是所谓的treeish。它是指项目历史中某一点的一种方式。Treeishes 可以是标签、提交、日期说明符、序数说明符或许多其他东西。
Now a branch
is just like a tag but is movable. When you are "on" a branch and make a commit, the branch is moved to the new commit you made indicating it's current position.
现在 abranch
就像一个标签,但可以移动。当您“在”一个分支并进行提交时,该分支将移动到您所做的新提交,表明它的当前位置。
Your HEAD
is pointer to a branch which is considered "current". Usually when you clone a repository, HEAD
will point to master
which in turn will point to a commit. When you then do something like git checkout experimental
, you switch the HEAD
to point to the experimental
branch which might point to a different commit.
你HEAD
是指向一个被认为是“当前”的分支的指针。通常当你克隆一个仓库时,HEAD
会指向master
哪个又会指向一个提交。当你做类似的事情时git checkout experimental
,你切换HEAD
到指向experimental
可能指向不同提交的分支。
Now the explanation.
现在解释。
When you do a git checkout v2.0
, you are switching to a commit that is not pointed to by a branch
. The HEAD
is now "detached" and not pointing to a branch. If you decide to make a commit now (as you may), there's no branch pointer to update to track this commit. Switching back to another commit will make you lose this new commit you've made. That's what the message is telling you.
当您执行 a 时git checkout v2.0
,您将切换到 a 未指向的提交branch
。现在HEAD
是“分离的”,而不是指向一个分支。如果您决定现在进行提交(就像您可能的那样),则没有要更新的分支指针来跟踪此提交。切换回另一个提交将使您丢失您所做的这个新提交。这就是消息告诉你的。
Usually, what you can do is to say git checkout -b v2.0-fixes v2.0
. This will create a new branch pointer at the commit pointed to by the treeish v2.0
(a tag in this case) and then shift your HEAD
to point to that. Now, if you make commits, it will be possible to track them (using the v2.0-fixes
branch) and you can work like you usually would. There's nothing "wrong" with what you've done especially if you just want to take a look at the v2.0
code. If however, you want to make any alterations there which you want to track, you'll need a branch.
通常,你能做的就是说git checkout -b v2.0-fixes v2.0
。这将在 treeish 指向的提交处创建一个新的分支指针v2.0
(在这种情况下是一个标签),然后将您的HEAD
to 指向那个。现在,如果您进行提交,则可以跟踪它们(使用v2.0-fixes
分支)并且您可以像往常一样工作。你所做的没有任何“错误”,特别是如果你只是想看看v2.0
代码。但是,如果您想在那里进行任何要跟踪的更改,则需要一个分支。
You should spend some time understanding the whole DAG model of git. It's surprisingly simple and makes all the commands quite clear.
您应该花一些时间了解 git 的整个 DAG 模型。它非常简单,并且使所有命令都非常清楚。
回答by KingCrunch
Yes, it is normal. This is because you checkout a single commit, that doesnt have a head. Especially it is (sooner or later) not a head of any branch.
是的,这是正常的。这是因为您签出了一个没有头部的提交。特别是它(迟早)不是任何分支的负责人。
But there is usually no problem with that state. You may create a new branch from the tag, if this makes you feel safer :)
但是这种状态通常没有问题。你可以从标签创建一个新分支,如果这让你感觉更安全:)