检查 Git 标签会导致“分离的 HEAD 状态”
我正在为我的 git 项目开发一个部署脚本,并且刚刚开始使用标签。我添加了一个名为 v2.0
的新标签:
git tag -a v2.0 -m "Launching version 2.0"
我已将此标签推送到远程存储库
git push --tags
当我尝试执行部署脚本并查看 v2.0
时, code> 标签我收到此消息:
您处于“分离 HEAD”状态。您可以环顾四周,进行实验性更改并提交它们,并且可以放弃任何提交 您在这种状态下执行不会影响任何分支 另一个结帐。如果你想创建一个新分支来保留提交 您创建后,您可以(现在或以后)通过使用 -b 和结帐来执行此操作 再次命令。示例: git checkout -b new_branch_name HEAD 现在是 在
这正常吗?存储库处于不确定状态,因为如果我这样做:
git branch
我会得到以下输出:
* (no branch)
master
抱歉,如果这很明显,但我无法弄清楚。
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 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:
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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,首先几个术语稍微过于简单化了。
在
git
中,标签
(就像许多其他东西一样)就是所谓的 树状。这是指代项目历史上某个时刻的一种方式。 Treeish 可以是标签、提交、日期说明符、序数说明符或许多其他东西。现在,分支就像标签一样,但可以移动。当您在分支“上”并进行提交时,该分支将移动到您所做的新提交,表明它的当前位置。
您的
HEAD
是指向被视为“当前”的分支的指针。通常,当您克隆存储库时,HEAD
将指向master
,而master
又将指向提交。然后,当您执行诸如 git checkoutexperimental 之类的操作时,您可以将 HEAD 切换为指向experimental 分支,该分支可能指向不同的提交。现在解释一下。
当您执行
git checkout v2.0
时,您将切换到分支
未指向的提交。HEAD
现在是“分离的”并且不指向分支。如果您决定现在进行提交(正如您可能的那样),则没有分支指针可以更新来跟踪此提交。切换回另一个提交将使您丢失所做的新提交。这就是消息要告诉你的。通常,您可以做的就是说
git checkout -b v2.0-fixes v2.0
。这将在树形v2.0
(本例中为标记)指向的提交处创建一个新的分支指针,然后将您的HEAD
移动到指向该指针。现在,如果您进行提交,就可以跟踪它们(使用v2.0-fixes
分支),并且您可以像平常一样工作。您所做的事情没有任何“错误”,特别是如果您只想看一下v2.0
代码。但是,如果您想在那里进行任何想要跟踪的更改,则需要一个分支。你应该花一些时间来理解 git 的整个 DAG 模型。它非常简单,并且所有命令都非常清晰。
Okay, first a few terms slightly oversimplified.
In
git
, atag
(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.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.Your
HEAD
is pointer to a branch which is considered "current". Usually when you clone a repository,HEAD
will point tomaster
which in turn will point to a commit. When you then do something likegit checkout experimental
, you switch theHEAD
to point to theexperimental
branch which might point to a different commit.Now the explanation.
When you do a
git checkout v2.0
, you are switching to a commit that is not pointed to by abranch
. TheHEAD
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.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 treeishv2.0
(a tag in this case) and then shift yourHEAD
to point to that. Now, if you make commits, it will be possible to track them (using thev2.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 thev2.0
code. If however, you want to make any alterations there which you want to track, you'll need a branch.You should spend some time understanding the whole DAG model of git. It's surprisingly simple and makes all the commands quite clear.
是的,这很正常。这是因为您签出一个没有头部的提交。尤其是它(迟早)不是任何分支的领导者。
但那种状态通常是没有问题的。您可以从标签创建一个新分支,如果这让您感觉更安全:)
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 :)