Git Tagging

How to create and handle tags in Git.

Creating tags

There are two types of tags, lightweight and annotated. The following explanation was taken from a Stack Overflow post:

A lightweight tag is very much like a branch that doesn’t change - it’s just a pointer to a specific commit.

Annotated tags, however, are stored as full objects in the Git database. They’re checksummed; contain the tagger name, email, and date; have a tagging message; and can be signed and verified with GNU Privacy Guard (GPG). It’s generally recommended that you create annotated tags so you can have all this information; but if you want a temporary tag or for some reason don’t want to keep the other information, lightweight tags are available too.

Create lightweight tag

git tag TAG

Create annotated tag

git tag -a TAG -m COMMIT MESSAGE

Handling tags

Pushing new tags

git push origin TAG

Deleting local tags

git tag -d TAG

Deleting remote tags

git push origin --delete TAG

Listing tags

git tag

Sorting output

Use the sort switch --sort=<type>, where type can be:

  • refname: lexicographic order
  • version:refname or v:refname: tag names are treated as versions

Examples

Lexical sort

$ git tag -l --sort=refname "foo*"
foo1.10
foo1.3
foo1.6

Version sort

$ git tag -l --sort=version:refname "foo*"
foo1.10
foo1.6
foo1.3

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
Last modified March 20, 2023: Styling of code blocks (ac16cba)