如何在Git仓库中的所有分支中找到最新的标签?

3
我想知道如何在git仓库中查找所有分支中最近的标签。我尝试使用以下命令,但它只检查当前分支:
git describe --abbrev=0 --tags | sed 's/[^0-9.]*//g'

然而,我希望能够检查所有分支。


1
定义“最近”。您是指时间上最后创建的那个?还是指拓扑上最接近某个分支末端的那个? - Lasse V. Karlsen
1个回答

1
这是一段 Bash 脚本。

#!/bin/bash

#list all the tags 
git for-each-ref --shell refs/tags |
  awk '{
#transform the object name into the commit date as a Unix epoch timestamp 
    "git log -1 --pretty=%cd --date=unix "$1 | getline $1;
#if the tag does not refer to a commit, ignore it
    if($0 ~ /^[0-9a-f]/) print;
#sort by the timestamp reversely, from the youngest to the oldest
  }' | sort -r |
#get the first youngest tag
  head -1 | awk '{print $NF}' |
#get all the tags that point at this tag in case there are multiple youngest tags, 
#with a side effect to remove "refs/tags/"
  xargs -i git tag --points-at {}

一行版本:

git for-each-ref --shell refs/tags | awk '{"git log -1 --pretty=%cd --date=unix "$1 | getline $1;if($0 ~ /^[0-9a-f]/) print;}' | sort -r |head -1 | awk '{print $NF}' | xargs -i git tag --points-at {}

如果您使用的是Git v2.9.4之前的版本,请使用--date=iso而不是--date=unix。使用--date=iso可能存在一个bug,由于时区的原因,时间戳无法按预期排序。但我认为这种情况很少发生。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接