首页 > 解决方案 > 在远程 git 存储库上查找标签的分支

问题描述

我正在尝试使用 gui 自动化构建步骤,并希望创建一个包含所有可用标签和分支头的 json 文件以供选择。我已经可以通过使用列出存储库中的所有分支和标签

git ls-remote --tags/heads url-of-repo

现在我想知道,哪个标签属于一个分支。我可以做类似的事情

git branch --contains tags/<tag>

但我想避免在本地检查所有存储库以获取此信息。也许有一个命令可以直接显示一个分支的所有标签?

标签: gittagsbranch

解决方案


做一个克隆,一个光秃秃的,

git clone --bare url-of-repo

每次要列出分支时,先更新分支和标签,

cd path-to-the-bare-repo
git fetch origin +refs/heads/*:refs/heads/* +refs/tags/*:refs/tags/*

然后运行

git branch --contains tags/<tag>

我推荐git for-each-ref,它允许格式化输出并且对脚本更友好。例如,

git for-each-ref refs/heads --contains tags/<tag> --format="%(refname:lstrip=2)"

如果不想cd在脚本中使用,也可以导出GIT_DIR并取消设置。

export GIT_DIR=path-to-the-bare-repo
git fetch origin +refs/heads/*:refs/heads/* +refs/tags/*:refs/tags/*
git for-each-ref refs/heads --contains tags/<tag> --format="%(refname:lstrip=2)"
unset GIT_DIR

或者干脆

GIT_DIR=path-to-the-bare-repo git fetch origin +refs/heads/*:refs/heads/* +refs/tags/*:refs/tags/*
GIT_DIR=path-to-the-bare-repo git for-each-ref refs/heads --contains tags/<tag> --format="%(refname:lstrip=2)"

如果您不想进行本地克隆,并且您可以访问托管服务器。另一种选择是运行 Web 服务来查询服务器中托管的存储库中的分支。您可以django在几分钟内编写和运行这样的服务。


推荐阅读