git - 为什么 git 标签没有出现在任何分支上?

标签 git github tags git-branch

我克隆了 mosquitto repo有标签 v1.4.9 .但是,标记的提交似乎不在分支上。

怎么会这样?作者是否真的在自己的 repo 上保留了一个分支,但只将标签从该分支推送到 GitHub?或者他只是对标签做出 promise ?

我把tag做成了本地分支

$ git checkout -b work149 v1.4.9

并查看分支上的最后一次提交:

$ git log -1
commit 91bfd82491f90e24b6fe9c036f0b04a1f5c14a89
Merge: bf959ef 2d0af73
Author: Roger A. Light <roger@atchoo.org>
Date:   Thu Jun 2 22:05:34 2016 +0100

    Merge branch 'fixes'

此提交是 fixes 分支之前的提交。

使用 git log --graph 我可以看到同一分支上的早期提交(不是 fixes 分支,而是我试图理解的分支):

* |   commit bf959ef9b0ae0e4d74bf80158ffb0b7c69da533d
|\ \  Merge: 646e0a0 5cca6b4
| |/  Author: Roger A. Light <roger@atchoo.org>
| |   Date:   Sun Feb 14 14:38:42 2016 +0000
| |
| |       Merge branch 'fixes'
| |

如何知道一个标签是否在一个分支上,在哪个分支上?最左边的垂直条是否表示一个分支,该分支在 Remote 上在哪里?

这是一种常见的做法吗?

discussion thread “Git pull doesn’t get the tags”提到“正在跟踪的分支负责人”和“未提交”。我想知道 git clone 命令是否将克隆配置为不跟踪远程上的所有分支,或者 repo 是否以某种方式将标签设为非提交?

最佳答案

我猜作者可能有一个包含 91bfd82491f 的分支,标记那个提交,推送标签,然后删除分支。你也是正确的,作者可能有一个本地分支指向同一个提交,但只推送标签,而不是分支。

使用

检查哪个分支或哪些分支包含v1.4.9
git branch -a --contains v1.4.9

运行该命令没有输出,这表明它不在自己的分支上。相反,寻找v1.4.8:

$ git branch -a --contains v1.4.8
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/debian
  remotes/origin/master

在任何分支之外直接创建标记提交的一种方法是使用 detached HEAD 操作,这就是 HEAD 不引用命名分支的地方。在蚊子克隆中,你可以通过运行到达那里

git checkout v1.4.9

这会给你一个喋喋不休的警告。

Note: checking out 'v1.4.9'.

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 91bfd82... Merge branch 'fixes'

At this point, git will create more commits. For example:

$ touch look-ma-no-branch ; git add look-ma-no-branch

$ git commit -m 'Look, Ma! No branch!'
[detached HEAD 51a0ac2] Look, Ma! No branch!
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 look-ma-no-branch

这个新提交 51a0ac2 不存在于任何分支上,我们可以确认这一点。

$ git branch -a --contains 51a0ac2
* (HEAD detached from v1.4.9)

为了好玩,我们也给它打上标签。

git tag -a -m 'Tag branchless commit' v1.4.9.1

git checkout master切换回master分支,我们可以使用git lola (git log --graph --decorate --pretty=oneline --abbrev-commit --all 的别名)以查看新标签与其前身相似。

$ git lola
* 51a0ac2 (tag: v1.4.9.1) Look, Ma! No branch!
*   91bfd82 (tag: v1.4.9) Merge branch 'fixes'
|\
| | * 1cd4092 (origin/fixes) [184] Don't attempt to install docs when WITH_DOCS=no.
| | * 63416e6 ;
| | * 5d96c3d [186] Fix TLS operation with websockets listeners and libwebsockts 2.x.
| |/
| * 2d0af73 Bump version number.
| | *   8ee1ad8 (origin/coverity-fixes) Merge branch 'fixes' into coverity-fixes
[...]

Confirm that it exists on no branch using

git branch -a --contains v1.4.9.1

因为你问了,不,这根本不是一个常见的 git 工作流程。

关于git - 为什么 git 标签没有出现在任何分支上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37666246/

相关文章:

gitignore 除特定文件外的所有内容

git - Jenkins - 未加载 git 全局配置

git - 如何知道对 master 的最新提交是否已推送到远程?

java - 在 doStartTag 函数中获取 JSP TLIB 的自定义标记名称

html - CSS:选择器属性继承

git - 如何从 refs/remotes 克隆一个包含所有分支和标签的 git repo?

Windows Git Bash 空白别名问题

python - 如何创建在 PyPI 上显示图片的 README.md?

git - 在 monorepo 中限制用户的访问权限

postgresql - 如何将 Hstore 键和值分解为 postgres 中的单独列