Home > Enterprise >  Git: How to sort tags by the date of the corresponding commit?
Git: How to sort tags by the date of the corresponding commit?

Time:03-31

Lightweight tags can be sorted by the date of the corresponding commit using (based on this answer)

git tag --sort=authordate

Annotated tags can be sorted by the date of the corresponding commit using (based on this answer)

git tag --sort=*authordate

It is possible to specify several sort fields:

git tag --sort=authordate --sort=*authordate --format='[%(*authordate:iso)][%(authordate:iso)] %(refname:short)'

but that groups all lightweight tags together and all annotated tags together because *authordate is empty for lightweight tags and authordate is empty for annotated tags. So I guess I would need the possibility to specify a fallback field which is used instead of another field if the other field is empty. Does git have a feature like that? (I haven't found it in git tag --help.)

How do I sort all tags (a mix of annotated tags and lightweight tags) by the authordate of the corresponding commit?

CodePudding user response:

Quickest way to get this done cleanly is probably

git tag --format='%(objectname)^{}' \
| git cat-file --batch-check \
| awk '$2=="commit" { print $1 }' \
| git log --stdin --author-date-order --no-walk --decorate --oneline

because tags don't have to point at commits, you can tag anything. *authordate of a tag pointing to a tag will also be blank despite it (usually) eventually resolving to a commit.

  • Related