Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Getting latest tag on git repository
# The command finds the most recent tag that is reachable from a commit.
# If the tag points to the commit, then only the tag is shown.
# Otherwise, it suffixes the tag name with the number of additional commits on top of the tagged object
# and the abbreviated object name of the most recent commit.
git describe
# With --abbrev set to 0, the command can be used to find the closest tagname without any suffix:
git describe --abbrev=0
# other examples
git describe --abbrev=0 --tags # gets tag from current branch
git describe --tags `git rev-list --tags --max-count=1` # gets tags across all branches, not just the current branch
@Steve973

This comment has been minimized.

Copy link

commented Sep 6, 2017

Thank you for this. I searched google to see how to find the latest tag on a branch. Your link was the first one I clicked on, and your explanation worked perfectly! Cheers!

@yuvaraj123

This comment has been minimized.

Copy link

commented May 21, 2018

Works at first attempt..Thanks

@Inkognitoo

This comment has been minimized.

Copy link

commented Jul 11, 2018

Thank you!

@dchorian

This comment has been minimized.

Copy link

commented Jul 13, 2018

Exactly what I was looking for! Thanks!

@lstomberg

This comment has been minimized.

Copy link

commented Aug 23, 2018

Thanks for this! Well documented.

@arekm

This comment has been minimized.

Copy link

commented Aug 25, 2018

But how to do that without checkouting repository? There is "git ls-remote --tags --sort="v:refname" git://github.com/git/git.git" but no way to sort by tag creation date it seems.

@coleHafner

This comment has been minimized.

Copy link

commented Feb 8, 2019

Works great for me. Thank you!

@KiltedCode

This comment has been minimized.

Copy link

commented Mar 27, 2019

Thank you for this! We were having issues where for some reason git describe --abbrev=0 --tags was no longer returning the appropriate latest annotated tag, but the last example works.

@ppetpadriew

This comment has been minimized.

Copy link

commented Jun 3, 2019

Thank man!

@stormmore

This comment has been minimized.

Copy link

commented Sep 2, 2019

But how to do that without checkouting repository? There is "git ls-remote --tags --sort="v:refname" git://github.com/git/git.git" but no way to sort by tag creation date it seems.

instead of --sort="v:refname", use --sort=taggerdate or --sort=-taggerdate depending on the ascending or descending requirement. That said, probably will only work on annotated tags.

@aryehbeitz

This comment has been minimized.

Copy link

commented Sep 8, 2019

this did it better.

git tag | sort -V | tail -1
@Ghost---Shadow

This comment has been minimized.

Copy link

commented Sep 20, 2019

this did it better.

git tag | sort -V | tail -1

This does not work

0.1.9 should come before 0.1.10

The sort just sorts alphabetically

@tavo-sanchez

This comment has been minimized.

Copy link

commented Sep 26, 2019

This does not work for me either. I have a list of tags like the one below

$git tag
v1.1.1
v1.1.2
v1.1.3
v1.2.0-rc.1
v1.2.0-rc.2
v1.2.0-rc.3
v1.2.0-rc.4
v1.2.0-rc.5
v1.2.0-rc.6
v1.2.0-rc.7

The tags RC are generated automatically, then I am tagging manually for the release version -> git tag v1.2.0 and doing a git describe I am expecting the v1.2.0 since it is the tag I just added. However, I am getting the v1.2.0-rc.7

Any idea why?

$git describe
v1.2.0-rc.7

$git tag
v0.1.0-rc.1
v0.1.0-rc.2
v0.1.0-rc.3
v1.0.0
v1.1.0
v1.1.0-rc.1
v1.1.1
v1.1.2
v1.1.3
v1.2.0           <- here is the lastest tag I added. As per the git documentation it lists the tags in alphabetical order which makes senses
v1.2.0-rc.1
v1.2.0-rc.2
v1.2.0-rc.3
v1.2.0-rc.4
v1.2.0-rc.5
v1.2.0-rc.6
v1.2.0-rc.7

The tag v1.2.0-rc.7 and v1.2.0 are on the same commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.