为什么 git 标签没有出现在任何分支上? [英] Why doesn’t a git tag show up on any branch?

查看:41
本文介绍了为什么 git 标签没有出现在任何分支上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I cloned the mosquitto repo that has tag v1.4.9. However the tagged commit does not appear to be on a branch.

怎么会这样?作者是否真的在自己的仓库中保留了一个分支,但只将标签从该分支推送到 GitHub?还是他只是对标签做出承诺?

How could that happen? Does the author actually keep a branch on his own repo but only push tags from that branch to GitHub? Or does he just make a commit to a tag?

我把标签做成了本地分支

I made the tag into a local branch

$ git checkout -b work149 v1.4.9

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

and looked at the last commit on the branch:

$ 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 分支之前提交.

This commit is one ahead of the fixes branch.

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

With git log --graph I can see an earlier commit on the same branch (not the fixes branch, but a branch I'm trying to understand):

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

你如何找出一个标签是否在一个分支上以及在哪个分支上?最左边的垂直条是否表示分支以及遥控器上的分支在哪里?

How do you find out whether a tag is on a branch and on which branch? Does the leftmost vertical bar indicate a branch and where is that branch on the remote?

这是一种常见的做法吗?

Is this a common practice?

讨论帖Git pull 没有得到标签" 提到了被跟踪的分支头"和未提交".我想知道 git clone 命令是否将克隆配置为不跟踪远程上的所有分支,或者 repo 以某种方式使标签变为非提交?

The discussion thread "Git pull doesn’t get the tags" mentions "branch heads that are being tracked" and "non-commits." I wonder whether the git clone command configures the clone not to track all the branches on the remote or the repo has somehow made the tags into non-commits?

推荐答案

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

My guess is the author probably had a branch that contained 91bfd82491f, tagged that commit, pushed the tag, and then later deleted the branch. You are also correct that the author may have a local branch that points to the same commit but pushed the tag only, not the branch.

检查哪个或哪些分支包含 v1.4.9 使用

Check which branch or branches contain v1.4.9 using

git branch -a --contains v1.4.9

运行该命令没有输出,这确认它不在自己的分支上.相比之下,寻找v1.4.8:

Running that command gives no output, which confirms that it is not on a branch of its own. In contrast, look for v1.4.8:

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

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

One way to directly create a tagged commit outside any branch is to operate with a detached HEAD, that is where HEAD does not refer to a named branch. In the mosquitto clone, you can get there by running

git checkout v1.4.9

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

which gives you a chatty warning.

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'

此时,git 会创建更多的提交.例如:

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 在任何分支上都不存在,我们可以确认.

This new commit 51a0ac2 does not exist on any branch, which we can confirm.

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

为了好玩,我们也给它加个标签吧.

For fun, let’s tag it too.

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) 以查看新标签看起来与其祖先相似.

Switching back to the master branch with git checkout master, we can use git lola (an alias for git log --graph --decorate --pretty=oneline --abbrev-commit --all) to see that the new tag looks similar to its progenitor.

$ 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 工作流程.

Because you asked, no, this is not at all a common git workflow.

这篇关于为什么 git 标签没有出现在任何分支上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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