Git describe with git tags
Getting tags on a git repository¶
Use git describe --tags
to get the tags.
Using git describe¶
Examples using git describe
Using git describe --tags
:
Using git describe
Using git describe --abbrev=0 --tags
to get the tag from the current branch
Using git describe
with git rev-list
to get tags across all branches, not just the current branch
Other examples¶
Other git describe examples (click to expand)
### Other examples using git describe Get the latest tagged version and remove the `v` prefix: Using `git describe` with `--abbrev` set to `0`Sorting tags¶
Using the --sort
option¶
From https://stackoverflow.com/questions/14273531/how-to-sort-git-tags-by-version-string-order-of-form-rc-x-y-z-w?#answer-22634649
With Git 2.0 (June 2014), you will be able to specify a sorting order!
Using --sort=<type>
Sort in a specific order. Supported type is: * "
refname
" (lexicographic order), * "version:refname
" or "v:refname
" (tag names are treated as versions).Prepend "
-
" to reverse sort order.
So, if you have:
Here is what you would get:
# lexical sort
git tag -l --sort=refname "foo*"
foo1.10
foo1.3
foo1.6
# version sort
git tag -l --sort=version:refname "foo*"
foo1.3
foo1.6
foo1.10
# reverse version sort
git tag -l --sort=-version:refname "foo*"
foo1.10
foo1.6
foo1.3
# reverse lexical sort
git tag -l --sort=-refname "foo*"
foo1.6
foo1.3
foo1.10
Examples using git tag to sort tags¶
Sort by -taggerdate
Sort by taggerdate
Sort by -committerdate
Sort by committerdate
Getting the latest tag¶
You can use any of the following commands to get the latest tag
Using git ls-remote
¶
Using git ls-remote --tags --sort=committerdate
:
git ls-remote --tags --sort=committerdate | grep -o 'v.*' | sort -r
## output
From https://github.com/rwaight/actions.git
v0.2.1
v0.2.0
v0.2
v0.1.2^{}
v0.1.2
v0.1.1^{}
v0.1.1
v0.1.0
v0
Using git ls-remote --tags --sort=committerdate | grep -o 'v.*'
:
git ls-remote --tags --sort=committerdate | grep -o 'v.*'
## output
v0.1.1
v0.1.2
v0.1.0
v0.1.1^{}
v0.1.2^{}
v0.2.0
v0
v0.2
v0.2.1
Using git ls-remote --tags --sort=taggerdate | grep -o 'v.*' | sort -r
:
git ls-remote --tags --sort=taggerdate | grep -o 'v.*' | sort -r
## output
From https://github.com/rwaight/actions.git
v0.2.1
v0.2.0
v0.2
v0.1.2^{}
v0.1.2
v0.1.1^{}
v0.1.1
v0.1.0
v0
Using git tag
¶
Using git tag --sort=-taggerdate | tail -1
:
Using git tag --sort=committerdate | grep -o 'v.*' | sort -r
:
git tag --sort=committerdate | grep -o 'v.*' | sort -r
## output
v0.2.1
v0.2.0
v0.2
v0.1.2
v0.1.1
v0.1.0
v0
Using git tag --sort=committerdate | grep -o 'v.*' | sort -r | head -1
:
Using git for-each-ref
¶
Using git for-each-ref --sort=creatordate --format '%(refname) %(creatordate)' refs/tags
:
git for-each-ref --sort=creatordate --format '%(refname) %(creatordate)' refs/tags
## output
refs/tags/v0.1.0 Mon Mar 11 12:10:23 2024 -0500
refs/tags/v0.1.1 Mon Mar 11 13:37:32 2024 -0500
refs/tags/v0.1.2 Mon Mar 11 13:50:12 2024 -0500
refs/tags/v0 Mon Mar 11 20:00:06 2024 -0500
refs/tags/v0.2 Mon Mar 11 20:00:06 2024 -0500
refs/tags/v0.2.0 Mon Mar 11 20:00:06 2024 -0500
refs/tags/v0.2.1 Thu Mar 14 11:39:55 2024 -0500