如何在 GIT 中显示最后 N 个标签

How to display last N tags in GIT

有没有办法显示 git 中的最后 N 个标签?

我对模式不感兴趣,因为它可以改变。例如,假设我有这些标签,我想获取其中的最后 3 个:

v1.0.0
v1.0.1
v2.0.0
v2.1.0
v3.0.0

基于Pro Git看来这个是无法实现的。或者我遗漏了什么?

对此没有单一的命令。为此,您必须结合使用 describerev-list 命令。

git describe --tags $(git rev-list --tags --max-count=3)

从这里得到答案:

这可以通过 Git v 2.0.0 中引入的 git tag --sort 选项来完成。

注意:我使用减号 - 进行反向排序(默认是从旧到新)。

<number>替换为实际的自然数。

UNIX,Linux,OS X

git tag --sort=-version:refname | head -n <number>

来自man head

 head [-n count | -c bytes] [file ...]

This filter displays the first count lines or bytes of each of the specified files, or of the standard input if no files are specified. If count is omitted it defaults to 10.

Windows,UNIX 方式

  1. 安装Cygwin
  2. 查看 UNIX 的答案

Windows,原生方式

git tag --sort=-version:refname | Select -First <number>

(在 serverfault 上找到了有关 Select 命令的信息)

来自 git reference:

--sort=<type>

Sort in a specific order. Supported type is "refname" (lexicographic order), "version:refname" or "v:refname" (tag names are treated as versions). The "version:refname" sort order can also be affected by the "versionsort.prereleaseSuffix" configuration variable. Prepend "-" to reverse sort order. When this option is not given, the sort order defaults to the value configured for the tag.sort variable if it exists, or lexicographic order otherwise.

并且在远程存储库上(使用 Nick Volynkin 的方法):

git ls-remote -tq --sort=-version:refname origin | egrep -v '\^\{}$' | head -n <number>