检查 Git 标签会导致“分离的 HEAD 状态”

发布于 2024-10-31 03:58:00 字数 662 浏览 3 评论 0原文

我正在为我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亣腦蒛氧 2024-11-07 03:58:00

好的,首先几个术语稍微过于简单化了。

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, 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.

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 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.

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.

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.

You should spend some time understanding the whole DAG model of git. It's surprisingly simple and makes all the commands quite clear.

一直在等你来 2024-11-07 03:58:00

是的,这很正常。这是因为您签出一个没有头部的提交。尤其是它(迟早)不是任何分支的领导者。

但那种状态通常是没有问题的。您可以从标签创建一个新分支,如果这让您感觉更安全:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文