我想知道如何在git仓库中查找所有分支中最近的标签。我尝试使用以下命令,但它只检查当前分支:
git describe --abbrev=0 --tags | sed 's/[^0-9.]*//g'
然而,我希望能够检查所有分支。
git describe --abbrev=0 --tags | sed 's/[^0-9.]*//g'
然而,我希望能够检查所有分支。
#!/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 {}
--date=iso
而不是--date=unix
。使用--date=iso
可能存在一个bug,由于时区的原因,时间戳无法按预期排序。但我认为这种情况很少发生。