Git 使用 -b 检查较旧的标签并提取最新的提交

标签 git git-branch git-tag

我将以此作为序言,我对 git 相当陌生,已经从 SVN 背景迁移。我一直在使用基于 git-flow 的工作流程,并使用 v0.2.0 等数字标准标记版本

今晚,我从沙箱部署到主站点,并注意到如果我执行了 git checkout -b tags/v0.2.2它实际上从主分支中提取了最新的提交。但是,如果我在分离的头状态下检查相同的标签,即 git checkout /tags/v0.2.2它根据正确的提交从 master checkout 。

[ calllog]$ git branch
* master
[ calllog]$ git checkout tags/v0.2.2
Note: checking out 'tags/v0.2.2'.

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 d5b4c5e... Merge branch 'feature/password' into develop
[ calllog]$ git describe
v0.2.2
[ calllog]$ git checkout master
Previous HEAD position was d5b4c5e... Merge branch 'feature/password' into develop
Switched to branch 'master'
[ calllog]$ git checkout -b tags/v0.2.2
Switched to a new branch 'tags/v0.2.2'
[ calllog]$ git describe
v0.2.4-5-gb2cfa6a

此行为似乎仅在检查非最新标签时才会发生

[calllog]$ git tag
v0.1.1
v0.1.2
v0.1.3
v0.1.4
v0.2.0
v0.2.1
v0.2.2
v0.2.3
v0.2.4

我错过了什么还是这是正常行为?从基于 git-flow 标记的工作流程升级到生产环境的推荐范例是什么?

最佳答案

[ calllog]$ git checkout -b tags/v0.2.2

这就是你的问题。 checkout -b说创建一个“我正在检查的新分支”,下一个参数是新分支的名称。您可以在对该命令的响应中看到它:

Switched to <b>a new branch</b> 'tags/v0.2.2'

您给了它新的分支名称,它成功了,但您实际上并没有 checkout 分支。 git 将无分支 checkout 视为“a glorified no-op with a rather expensive side-effects to show only the tracking information, if exists, for the current branch ”,因此您的新分支基于您当前的 checkout 。

那么为什么要采用这种方式进行无分支机构 checkout 呢?因为大多数分行都已关闭当前 checkout 。

你想要的是

git checkout -b $newbranch v0.2.2 

(编辑:我发现您的标签没有 tags/ 前缀,您希望基于它们的新分支具有该前缀。修复。)

关于Git 使用 -b 检查较旧的标签并提取最新的提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24155921/

相关文章:

git - 统一同一存储库的两个本地副本的分支

git - 如何避免本地分支机构的意外 dcommit

windows - 如何克隆文件名中带有冒号的文件

linux - yum install 无法在没有 root 权限的集群上运行

git - 如何使用 git 安装 coq contribs?

git add *(星号)与 git add 。 (时期)

git - 在 git merge 时总是忽略某个提交

Git 标签 checkout 和 git hook

git 提交修改而不丢失标签

version-control - 如何从 http ://hub. darcs.net 处的标签分支?