从远程仓库获取最后一个 git 标签,无需克隆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10649814/
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 13:43:42  来源:igfitidea点击:

Get last git tag from a remote repo without cloning

gitgit-taggit-describe

提问by fl00r

How to get last tag from a (non checked-out) remote repo?

如何从(未签出的)远程仓库获取最后一个标签?

On my local copy I use describe

在我的本地副本上,我使用 describe

git describe --abbrev=0 --tags

But I cannot use describewith remote storage

但我不能describe与远程存储一起使用

回答by Potherca

Use git ls-remote --tags <repository>

git ls-remote --tags <repository>

For example, if we want to know what the latest tag that Git is at we would do

例如,如果我们想知道 Git 所在的最新标签是什么,我们会这样做

git ls-remote --tags git://github.com/git/git.git

That returns a long list with all the tags in alphabetical order, as shown below (truncated for sanity's sake). The last line tells us the latest tag is v1.8.0-rc0.

这将返回一个长列表,其中包含按字母顺序排列的所有标签,如下所示(为了理智而被截断)。最后一行告诉我们最新的标签是v1.8.0-rc0

Keep in mind that tags can be any kind of string so, as pointed out by Christopher Gervaisin his answer, git ls-remotesorts tags alphabetically. Unfortunately git ls-remotedoes not have a --sortoption (like, for example git for-each-ref), so your best option is to use native sort.

请记住,标签可以是任何类型的字符串,因此正如Christopher Gervais他的回答中指出的那样,git ls-remote字母顺序对标签进行排序。不幸的是git ls-remote,没有--sort选项(例如git for-each-ref),因此您最好的选择是使用 native sort

More recent versions of sortsupport the -Vor --version-sortflag to do a natural sort of (version) numbers within text.

最新版本的sort支持-V--version-sort标志在文本中进行自然排序(版本)编号。

So to sort them naturally, your command would look like this:

因此,要自然对它们进行排序,您的命令将如下所示:

git ls-remote --tags git://github.com/git/git.git | sort -t '/' -k 3 -V

Please take a look at (and upvote) Christopher Gervais's answerbelow if you need more/other greping options.

如果您需要更多/其他选项,请查看(并投票)下面的Christopher Gervais 的回答grep



     ...
 e4dc716b1cfefb0e1bd46c699d4f74009118d001   refs/tags/v1.7.9
 828ea97de486c1693d6e4f2c7347acb50235a85d   refs/tags/v1.7.9^{}
 cc34c0417dfd4e647e41f3d34a032b7164aadea7   refs/tags/v1.7.9-rc0
 eac2d83247ea0a265d923518c26873bb12c33778   refs/tags/v1.7.9-rc0^{}
 ad2ec9a47a031ebf056444a94bea3750aaa68f63   refs/tags/v1.7.9-rc1
 6db5c6e43dccb380ca6e9947777985eb11248c31   refs/tags/v1.7.9-rc1^{}
 eab05abaeb51531e11835aaa4c26564a1babebac   refs/tags/v1.7.9-rc2
 bddcefc6380bd6629f3f12b5ffd856ec436c6abd   refs/tags/v1.7.9-rc2^{}
    ...
 5ace0b7af106b44687005085d8c252f8be9da5d3   refs/tags/v1.8.0-rc0
 b0ec16b49eb283156e13bbef26466d948e4fd992   refs/tags/v1.8.0-rc0^{}

回答by Ivan Bartsov

Since version 2.18Git hasa built-in --sortoption for the exact purpose of sorting ref names.

2.18版开始,Git一个内置--sort选项,用于对引用名称进行排序。

So the up-to-date command would be

所以最新的命令是

git ls-remote --tags --sort="v:refname" git://github.com/git/git.git | tail -n1

To also remove the hash and the dereference marker (^{}), just throw in some simple sed

要同时删除散列和取消引用标记 ( ^{}),只需添加一些简单的sed

git ls-remote --tags --sort="v:refname" git://github.com/git/git.git | tail -n1 | sed 's/.*\///; s/\^{}//'

As per suggestion by @Frederik Nord, you can also use the --refsswitch to get rid of the ^{}, which leaves just one sedcommand (making the oneliner 4 characters shorter):

根据@Frederik Nord 的建议,您还可以使用--refs开关来摆脱^{},只留下一个sed命令(使 oneliner 缩短 4 个字符):

git ls-remote --tags --refs --sort="v:refname" git://github.com/git/git.git | tail -n1 | sed 's/.*\///'

# outputs something like: v2.18.0

If you haven't git 2.18and want get the last tag version, try:

如果您还没有 git2.18并且想要获得最后一个标签版本,请尝试:

git ls-remote --tags git://github.com/git/git.git | sort -t '/' -k 3 -V | awk -F/ '{ print  }' | awk '!/\^\{\}/' | tail -n 1

# The output will be something like that: v2.27.0-rc0

回答by ergonlogic

Unfortuntely, git ls-remote --tagsactually lists tags alphabetically (at least as of 1.7.2.5). So, at the time that 1.7.10, 1.7.11 or 1.7.12 were the latest tags, 1.7.9 would have been the last on the list:

不幸的是,git ls-remote --tags实际上按字母顺序列出了标签(至少从 1.7.2.5 开始)。因此,在 1.7.10、1.7.11 或 1.7.12 是最新标签时,1.7.9 将是列表中的最后一个:

git ls-remote --tags git://github.com/git/git.git |grep "1\.7\."

[...]
bf68fe0313c833fa62755176f6e24988ef7cf80f        refs/tags/v1.7.9.6
cb2ed324fc917db0b79d7b1f3756575ffa5f70d5        refs/tags/v1.7.9.6^{}
3996bb24c84013ec9ce9fa0980ce61f9ef97be4d        refs/tags/v1.7.9.7
d0f1ea6003d97e63110fa7d50bb07f546a909b6e        refs/tags/v1.7.9.7^{}

However, we can pipe these results through 'sort' to get closer to the results we're looking for:

但是,我们可以通过“排序”来传递这些结果,以更接近我们正在寻找的结果:

git ls-remote --tags git://github.com/git/git.git |grep "1\.7\."| sort -g -k3 -t.

[...]
eab05abaeb51531e11835aaa4c26564a1babebac        refs/tags/v1.7.9-rc2
eac2d83247ea0a265d923518c26873bb12c33778        refs/tags/v1.7.9-rc0^{}
f59f511e26b4924b22c6966e79fe4f754bc81dc6        refs/tags/v1.7.9.2
0e2d57fd50f61e668be3180bc8f25991ea88aa8c        refs/tags/v1.7.10-rc1^{}
121f71f0da1bc9a4e1e96be2c3e683191a82a354        refs/tags/v1.7.10.4^{}
26e5c5d09334d157bd04f794f16f6e338d50c752        refs/tags/v1.7.10.3^{}
[...]
cffb45719f60d6fc2cc98ead6af88a895c63c9ac        refs/tags/v1.7.12.4
d8cf053dacb4f78920c112d10c7be21e4f5a5817        refs/tags/v1.7.12.2^{}
dcd07fb6262fd8bb9f531890df3986a8b719a0b5        refs/tags/v1.7.12-rc0
e15c16de396a1e1f42001b03cb885ce64eb4098e        refs/tags/v1.7.12-rc2^{}

While still not correct, it's closer. If we exclude -rc and ^{}, and add an additional sort on the last sub-version number, we can probably get close enough for most needs:

虽然仍然不正确,但已经接近了。如果我们排除 -rc 和 ^{},并在最后一个子版本号上添加额外的排序,我们可能可以满足大多数需求:

git ls-remote --tags git://github.com/git/git.git |grep "1\.7\."|grep -v -|grep -v {| sort -n -t. -k3 -k4

23ed9debf17263ed6bed478a4d6d86e71342c18a        refs/tags/v1.7.11.6
527b331100ddba839cc54bb31c1bcd66acc08321        refs/tags/v1.7.11.7
14d20a75e3d57a872a8c81ae90dcc4c61ddba011        refs/tags/v1.7.12
51993a414a76120fda20d56ba767fa513d9ff440        refs/tags/v1.7.12.1
04043f4d1ae42bddee67d354a2e6fd2464592a1e        refs/tags/v1.7.12.2
b38da673be332933b8f3a873ce46ffea08d2ee2c        refs/tags/v1.7.12.3
cffb45719f60d6fc2cc98ead6af88a895c63c9ac        refs/tags/v1.7.12.4

回答by Tom Hale

TL;DR:

特尔;博士:

% git -c 'versionsort.suffix=-' ls-remote -t --exit-code --refs --sort=-v:refname \
    https://github.com/robert7/nixnote2 'v*' \
    | sed -En '1!q;s/^[[:xdigit:]]+[[:space:]]+refs\/tags\/(.+)//gp'  
v2.1.0-beta4g

Explanation

解释

Pass --refsto git ls-remoteto get rid of the {}refs shown in other answers:

传递--refsgit ls-remote以摆脱{}其他答案中显示的参考:

$ git ls-remote -t --refs <URL>

This gives output such as:

这给出了如下输出:

8f235769a2853c415f811b19cd5effc47cc89433        refs/tags/continuous
24e666ed73486a2ac65f09a1479e91e6ae4a1bbe        refs/tags/continuous-develop
7c2cff2c26c1c2ad4b4023a975cd2365751ec97d        refs/tags/v2.0
35b69eed46e5b163927c78497983355ff6a5dc6b        refs/tags/v2.0-beta10

To get only the tag names, pass through:

要仅获取标签名称,请通过:

sed -E 's/^[[:xdigit:]]+[[:space:]]+refs\/tags\/(.+)/\1/g':

sed -E 's/^[[:xdigit:]]+[[:space:]]+refs\/tags\/(.+)/\1/g'

$ git ls-remote -t --exit-code --refs https://github.com/robert7/nixnote2.git \
  | sed -E 's/^[[:xdigit:]]+[[:space:]]+refs\/tags\/(.+)//g'
continuous
continuous-develop
v2.0
v2.0-beta10

You can then pass the cleaned up list through an appropriate grepand/or head -n1(or add to your sedcommand if you like keeping your PID numbers low.)

然后,您可以通过适当的grep和/或head -n1sed如果您希望保持较低的 PID 数字,则添加到您的命令中)传递清理过的列表。

Suggestions:

建议:

  • Add a pattern at the end of the command line to filter. Eg 'v*'if all version tags start with a v.
  • Pass --exit-codeto ensure a non-0exit when no matching refs are returned.
  • Use the https://version: it's faster and if you're packaging you don't want to run the risk of being asked for a ssh key.
  • --sort=-v:refnameto sort by version rather than lexographically, and have the largest versions at the top
  • Use git -c versionsort.suffix=-to prevent 2.0-rccoming "after" 2.0
  • 在命令行末尾添加模式以进行过滤。例如,'v*'如果所有版本标签都以v.
  • 当没有匹配的引用返回时,通过--exit-code以确保0不退出。
  • 使用该https://版本:它更快,而且如果您正在打包,您不想冒被要求提供 ssh 密钥的风险。
  • --sort=-v:refname按版本而不是按字典排序,并在顶部放置最大的版本
  • 使用git -c versionsort.suffix=-以防止2.0-rc未来的“后”2.0

回答by Vbp

This worked for me how to get latest tag from github remote repository

这对我有用如何从 github 远程存储库获取最新标签

git ls-remote --tags "#{github_repo}" | awk '{print }' | grep -v '{}' | awk -F"/" '{print }' | sort -n -t. -k1,1 -k2,2 -k3,3 | tail -n 1.chomp

回答by SebMa

Here is my one-liner :-)

这是我的单线 :-)

git ls-remote --tags --refs --sort="version:refname" git://github.com/git/git.git | awk -F/ 'END{print$NF}'