检出 Git 标签会导致“分离的 HEAD 状态"; [英] Checking out Git tag leads to "detached HEAD state"

查看:44
本文介绍了检出 Git 标签会导致“分离的 HEAD 状态";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的 git 项目开发部署脚本,我刚刚开始使用标签.我添加了一个名为 v2.0 的新标签:

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

当我尝试执行部署脚本并查看 v2.0 标记时,我收到以下消息:

When I try to execute the deployment script and check out the v2.0 tag I get this message:

您处于分离头"状态.您可以环顾四周,进行实验性更改并提交,也可以放弃任何提交您可以在不影响任何分支的情况下执行此状态另一个结帐.如果你想创建一个新的分支来保留提交您创建,您可以(现在或以后)通过在结帐时使用 -b再次命令.示例: git checkout -b new_branch_name HEAD 现在是在

这正常吗?存储库处于不确定状态,因为如果我这样做:

Is that normal? The repository is in limbo because if I do:

git branch

我得到这个输出:

* (no branch)
  master

对不起,如果这很明显,但我无法弄清楚.

Sorry if this is obvious but I couldn't figure it out.

推荐答案

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

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.

这篇关于检出 Git 标签会导致“分离的 HEAD 状态";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆