如何获取远程 git 中的最新标签列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20734181/
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
How to get list of latest tags in remote git?
提问by Psychozoic
There are alot of methods to get latest tags when you have local git repo.
当您拥有本地 git 存储库时,有很多方法可以获取最新标签。
But i want to get list of latest tags on remote repo.
但我想获取远程仓库上最新标签的列表。
I know about "git ls-remote", and everything is fine when you use tags like x.y.z (where x,y,z are numbers). But when tags looks like "test-x.y.z" and "dev-x.y.z" i noticed that large amount of "test" tags will pull out any new "dev" tags, which is not correct.
我知道“git ls-remote”,当你使用像 xyz 这样的标签时一切都很好(其中 x,y,z 是数字)。但是当标签看起来像“test-xyz”和“dev-xyz”时,我注意到大量的“test”标签会拉出任何新的“dev”标签,这是不正确的。
So, how would you like solve this?
那么,你想如何解决这个问题?
采纳答案by VonC
With Git 2.18 (Q2 2018), git ls-remote
learned an option to allow sorting its output based on the refnames being shown.
在 Git 2.18(2018 年第二季度)中,git ls-remote
学习了一个选项,允许根据显示的引用名称对其输出进行排序。
See commit 1fb20df(09 Apr 2018) by Harald Nordgren (HaraldNordgren
).
(Merged by Junio C Hamano -- gitster
--in commit 6c0110f, 08 May 2018)
请参阅Harald Nordgren ( ) 的commit 1fb20df(09 Apr 2018 )。(由Junio C Hamano合并-- --在commit 6c0110f,2018 年 5 月 8 日)HaraldNordgren
gitster
ls-remote
: create '--sort
' optionCreate a '
--sort
' option forls-remote
, based on the one fromfor-each-ref
.
This e.g. allows ref names to be sorted by version semantics, so thatv1.2
is sorted beforev1.10
.
ls-remote
: 创建 '--sort
' 选项创建一个 '
--sort
' 选项ls-remote
,基于来自for-each-ref
.
这例如允许 ref 名称按版本语义排序,因此v1.2
排序在 之前v1.10
。
So check out those for-each-ref --sort
options introduced in Git 2.0 and 2.8, because they apply now to git ls-remote --sort
.
因此,请查看for-each-ref --sort
Git 2.0 和 2.8 中引入的那些选项,因为它们现在适用于git ls-remote --sort
.
回答by cooperok
Do you use linux? If so you can use this command
你用linux吗?如果是这样,您可以使用此命令
git ls-remote --tags | grep -o 'refs/tags/dev-[0-9]*\.[0-9]*\.[0-9]*' | sort -r | head | grep -o '[^\/]*$'
It will show you 10 latest tags (with name dev-x.y.z)
它将显示 10 个最新标签(名称为dev-xyz)
UPD
You can use this bash script to get latest tags:
UPD
你可以使用这个 bash 脚本来获取最新的标签:
#!/bin/bash
TAGS=("dev-[0-9]*\.[0-9]*\.[0-9]*" "test-[0-9]*\.[0-9]*\.[0-9]*" "good-[0-9]*" "new [0-9][0-9][0-9]")
for index in ${!TAGS[*]}
do
git ls-remote --tags | grep -o "refs/tags/${TAGS[$index]}" | sort -rV | head | grep -o '[^\/]*$'
done
Just add in array TAGS regular expressions that you want, and you'll get 10 latest tags for every of them. If you want to get more or less tags, just add param -n to head command 'head -n 5' or 'head -n 15'.
只需添加您想要的数组 TAGS 正则表达式,您就会为每个标签获得 10 个最新标签。如果您想获得更多或更少的标签,只需将 param -n 添加到 head 命令 'head -n 5' 或 'head -n 15'。
Just in case. Save it in folder ~/bin (for example with name git_tags), then add executable permission (chmod +x git_tags), this will allow you to run this bash script from every place (just type git_tags).
以防万一。将其保存在文件夹 ~/bin 中(例如名称为 git_tags),然后添加可执行权限(chmod +x git_tags),这将允许您从任何地方运行此 bash 脚本(只需键入 git_tags)。
回答by Psychozoic
some guy told me that command:
有人告诉我这个命令:
git ls-remote -t repo.url.git | awk '{print }' | cut -d '/' -f 3 | cut -d '^' -f 1 | sort -b -t . -k 1,1nr -k 2,2nr -k 3,3r -k 4,4r -k 5,5r | uniq
and this is not the best solution, but he opened my eyes on command sort
.
这不是最好的解决方案,但他命令我睁开了眼睛sort
。
but i would like to know other versions.
但我想知道其他版本。
回答by f-society
git ls-remote --tags | awk -F'/' '/[0-9].[0-9].[0-9].*/ { print }' | sort -nr | head -n1
Using github api:
使用 github api:
curl https://api.github.com/repos/user/repo/tags | jq '.[] .name' | sort -nr | head -n1
These two will get you latest tag, you can increase the list by changing the value at n
flag for head pipe. lets say, to get top 10 latest lists head -n10
这两个将为您提供最新的标签,您可以通过更改n
头管标志处的值来增加列表。让我们说,要获得前 10 名的最新列表head -n10