且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检出 Git 标签会导致“分离的 HEAD 状态";

更新时间:2023-01-17 18:28:13

好的,首先几个术语稍微简化了一些.

Okay, first a few terms slightly oversimplified.

git 中,tag(像许多其他东西一样)就是所谓的 树形.这是指项目历史上的一个点的一种方式.Treeishes 可以是标签、提交、日期说明符、序数说明符或许多其他东西.

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.

现在 branch 就像一个标签,但可以移动.当您在"一个分支并进行提交时,该分支将移动到您所做的新提交,表明它的当前位置.

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.

您的 HEAD 是指向被视为当前"分支的指针.通常,当您克隆存储库时,HEAD 将指向 master,后者又将指向提交.然后,当您执行诸如 git checkout Experimental 之类的操作时,将 HEAD 切换为指向 experimental 分支,该分支可能指向不同的提交.

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.

现在解释.

当您执行 git checkout v2.0 时,您将切换到未由 branch 指向的提交.HEAD 现在是分离的"并且不指向分支.如果您决定现在进行提交(就像您可能的那样),则没有要更新的分支指针来跟踪此提交.切换回另一个提交将使您丢失您所做的这个新提交.这就是消息告诉你的.

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.

通常,你可以做的是说git checkout -b v2.0-fixes v2.0.这将在树状 v2.0(在本例中为标记)指向的提交处创建一个新的分支指针,然后将您的 HEAD 移动到指向该位置.现在,如果您进行提交,则可以跟踪它们(使用 v2.0-fixes 分支)并且您可以像往常一样工作.您所做的没有任何错误",特别是如果您只想查看 v2.0 代码.但是,如果您想在那里进行任何要跟踪的更改,则需要一个分支.

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 的整个 DAG 模型.它非常简单,并且使所有命令都非常清楚.

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