How to display last N tags in GIT

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

I am not interested in a pattern because this can change. For example let’s say I have these tags and I want to get last 3 of them:

  • Is it possible to remove a remote repository on Github through the command line?
  • change the data directory gitlab to store repos elsewhere
  • Execute git commit from php file in current directory
  • Git pull is always invoking merge
  • How do I remove a file that was checked in a few commits back using Git?
  • Git Commit Error terminal with the editor
  • v1.0.0

    Based on the Pro Git seems that this cannot be achieved. Or I am missing something?

  • Merge git repo into branch of another repo
  • Git error Permission denied (publickey), couldn't paste publickey
  • Git - Cannot delete remote branch
  • Test to determine if git clone command succeeded
  • can we Commit on Branch that's already been merged with master?
  • Pros and cons of using git revisions instead of branches as references for rollouts
  • 2 Solutions collect form web for “How to display last N tags in GIT”

    This can be done with git tag --sort option which is introduced in Git v 2.0.0.

    Note: I’m using the minus sign - to get reverse sorting order (default is oldest to newest).

    Replace <number> with an 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 of the standard input if no files are specified.
    If count is omitted it defaults to 10.

    Windows, the UNIX way

    1. Install Cygwin
    2. See answer for UNIX

    Windows, native way

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

    (Info on Select command was found on serverfault)

    From git reference:


    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.

    There is no single command for this. You have to use a combination of the describe and rev-list commands for this.

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

    Got the answer from here:

    Git Baby is a git and github fan, let's start git clone.