git 列出分支包含的标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2381665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:58:19  来源:igfitidea点击:

list tags contained by a branch

git

提问by tarsius

How can I list tags contained by a given branch, the opposite of:

如何列出给定分支包含的标签,与:

git tag --contains <commit>

Which "only list tags which contain the specified commit".

其中“仅列出包含指定提交的标签”。

If something like this does not exist, how do I test whether a commit is contained by another commit so that I can script this?

如果这样的东西不存在,我如何测试一个提交是否包含在另一个提交中,以便我可以编写它的脚本?

I could do this:

我可以这样做:

commit=$(git rev-parse $branch)
for tag in $(git tag)
do
    git log --pretty=%H $tag | grep -q -E "^$commit$"
done

But I hope there is a better way as this might take a long time in a repository with many tags and commits.

但我希望有更好的方法,因为这可能需要在具有许多标签和提交的存储库中花费很长时间。

采纳答案by Chris Johnsen

This might be close to what you want:

这可能接近您想要的:

git log --simplify-by-decoration --decorate --pretty=oneline "$committish" | fgrep 'tag: '

But, the more common situation is to just find the most recent tag:

但是,更常见的情况是只找到最近的标签:

git describe --tags --abbrev=0 "$committish"
  • --tagswill search against lightweight tags, do not use it if you only want to consider annotated tags.
  • Do not use --abbrev=0if you want to also see the usual “number of ‘commits on top' and abbreviated hash” suffix (e.g. v1.7.0-17-g7e5eb8).
  • --tags将搜索轻量级标签,如果您只想考虑带注释的标签,请不要使用它。
  • --abbrev=0如果您还想查看通常的“顶部提交次数和缩写哈希”后缀(例如 v1.7.0-17-g7e5eb8),请不要使用。

回答by JamHandy

git tag --merged <branch>

From the man page:

从手册页:

--[no-]merged <commit>

Only list tags whose tips are reachable, or not reachable if --no-merged is used, from the specified commit (HEAD if not specified).

I believe this option was added quite recently - it definitely wasn't available back when the original question was posed and the above answers suggested. Since this thread is still the first hit in Google for the question I figured I'd throw it in for anyone who scrolls down to the bottom looking for an answer that involves less typing than the accepted answer (and for my own reference when I forget this answer again next week).

我相信这个选项是最近添加的 - 当提出原始问题并建议上述答案时,它肯定无法恢复。由于该线程仍然是 Google 中第一个针对该问题的热门话题,我想我会将它扔给向下滚动到底部寻找比接受的答案输入更少的答案的任何人(以及当我忘记时供我自己参考下周再次回答这个问题)。

回答by Ferry Huberts

to list all tags reachable on the current branch:

列出当前分支上可访问的所有标签:

git log --decorate=full --simplify-by-decoration --pretty=oneline HEAD | \
sed -r -e 's#^[^\(]*\(([^\)]*)\).*$##' \
       -e 's#,#\n#g' | \
grep 'tag:' | \
sed -r -e 's#[[:space:]]*tag:[[:space:]]*##'

回答by akutz

I do not have enough reputation to comment on other people's posts, but this is in response to the answer and its comments at https://stackoverflow.com/a/7698213/2598329. In order to show all of the tags reachable by the current branch, including the tag on the HEAD commit, you can use the following:

我没有足够的声誉来评论其他人的帖子,但这是对https://stackoverflow.com/a/7698213/2598329上的答案及其评论的回应。为了显示当前分支可访问的所有标签,包括 HEAD 提交上的标签,您可以使用以下命令:

git log --decorate --oneline | egrep '^[0-9a-f]+ \((HEAD, )?tag: ' | ssed -r 's/^.+tag: ([^ ]+)[,\)].+$//g'

One caveat - I use super sed, so you may need to change my "ssed" to sed.

一个警告 - 我使用超级 sed,因此您可能需要将我的“ssed”更改为 sed。

And for the hell of it, here it is in PowerShell:

更糟糕的是,它在 PowerShell 中:

git log --decorate --oneline | % { if ($_ -match "^[0-9a-f]+ \((HEAD, )?tag: ") { echo $_} } | % { $_ -replace "^.+tag: ([^ ]+)[,\)].+$", "`" }

-- -a

- -一种

回答by akhan

Here's how I list annotated tags matching a pattern (TAG_PREFIX*) in reverse chronological order. This recipe uses git-describe.

下面是我如何按时间倒序列出与模式 (TAG_PREFIX*) 匹配的带注释的标签。本食谱使用 git-describe。

#!/usr/bin/env bash

tag=$(git describe --abbrev=0 --match TAG_PREFIX*)
until [ -z $tag ]; do
    echo $tag
    tag=$(git describe --abbrev=0 --match TAG_PREFIX* $tag^ 2>/dev/null);
done

This won't work if multiple tags matching the pattern point to the same commit. For that, here is another recipe that uses git-rev-list and git-tag to list all tags, matching TAG_PREFIX*, starting from a commit (HEAD in this example).

如果匹配模式的多个标签指向同一个提交,这将不起作用。为此,这是另一个使用 git-rev-list 和 git-tag 列出所有标签的方法,匹配 TAG_PREFIX*,从提交开始(本例中为 HEAD)。

#!/usr/bin/env bash

git rev-list HEAD | while read sha1; do
    tags=( "$(git tag -l --points-at $sha1 TAG_PREFIX*)" )
    [[ ! -z ${tags[*]} ]] && echo "${tags[@]}" | sort -r
done

回答by jthill

Efficient for large numbers of tags and gets easy format flexibility:

高效处理大量标签并获得简单的格式灵活性:

{ git rev-list --first-parent ${1:-HEAD}
  git for-each-ref --format='%(objectname) %(objecttype)  %(refname)
                             %(*objectname) *(%(objecttype)) %(refname)' 
} \
| awk '
  NF==1   { revs[]=1; next }
          { if (  in revs ) print ,, }
'

which gets all refs, add refs/tagsto the f-e-r to restrict it.

获取所有引用,添加refs/tags到 fer 以限制它。

回答by ebneter

git describe(or some variant of it) might be what you're looking for.

git describe(或它的某些变体)可能是您正在寻找的。

回答by Jared Grubb

You could use this:

你可以用这个:

# get tags on the last 100 commits:
base_rev=master~100
end_rev=master
for rev in $(git rev-list $base_rev..$end_rev)
do 
    git describe --exact-match $rev 2> /dev/null
done

回答by fork0

There is a git branch --contains(Git since 1.5.7, at least)

有一个git branch --contains(至少从 1.5.7 开始是Git)