How to display the last N tags in GIT

Is there a way to display the last N tags in git?

I am not interested in the template, because it can change. For example, let's say I have these tags, and I want to get the last 3 of them:

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

Based on Pro Git, it seems like this cannot be achieved. Or am I missing something?

+6
source share
2 answers

This can be done using the git tag --sort , which is introduced in Git v 2.0.0.

Note. I use the minus sign - to get the reverse sort order (the oldest and newest by default).

Replace <number> with the actual natural number.

UNIX, Linux, OS X

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

From man head :

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

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

Windows UNIX path

  • Install Cygwin
  • See answer for UNIX

Windows native way

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

(Information about the Select command was found on serverfault )

From git link :

 --sort=<type> 

Sort in a specific order. The supported type is "refname" (lexicographic order), "version: refname" or "v: refname" (tag names are treated as versions). The sorting order of "version: refname" can also be affected by the configuration variable "versionort.prereleaseSuffix". Prepare a “-” to reverse the sort order. If this option is not specified, the default sort order corresponds to the value configured for the tag.sort variable, if one exists, or the lexicographic order otherwise.

+4
source

There is no single team for this. To do this, you must use a combination of the describe and rev-list commands.

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

Got a response from here: fooobar.com/questions/4969 / ...

+2
source

Source: https://habr.com/ru/post/989015/


All Articles