GitPython tags sorted GitPython tags sorted python python

GitPython tags sorted


The IterableList object returned by repo.tags in GitPython inherits from the list python class, which means that you can sort it the way you want. To get the latest tag created, you can simply do:

import gitrepo = git.Repo('path/to/repo')tags = sorted(repo.tags, key=lambda t: t.commit.committed_datetime)latest_tag = tags[-1]


I have just had a look and found the code responsible for the sorting. Therefore I see no other way but to reverse the sorting order yourself, like

reversed(repo.tags)

If preferred, you can also use the underlying git command, and parse the result yourself, such as in this example:

repo.git.tag(l=True) # equals git tag -l

That way, whatever git tag can do, you can do (which could be interesting for listing tags in order of traversal, starting from a given commit).


The above will work if there are different commits associated with the tags (which is generally the case). However a more accurate way would be as follows which picks up the tag date:

import gitrepo = git.Repo('path/to/repo')tags = sorted(repo.tags, key=lambda t: t.tag.tagged_date)latest_tag = tags[-1]