bash script to find out largest git tag name

Viewed 30

Say below command list all git tags, how to modify it to get the largest tag?

git tag

Current output is:

    V1.0.0
    V1.0.1
    V1.0.2
    ...
    V1.0.23

I expect the output is V1.0.23 for this example

1 Answers

Have Git sort them for you, by version, descending, and pick just the first one:

git tag --list --sort='-version:refname' | head -n1

Or pipe the tags to a sort that knows version sort (such as GNU sort):

git tag | sort -rV | head -n1
Related