Tags management in Git

There are two kinds of tags in Git:

  • Lightweight tags — just a tag name pointing to a specific commit.
  • Annotated tags — a object stored in the Git database. It contains the tagger name, email, date and a tag name and message. It can be signed and verified with GNU Privacy Guard(GPG).

Generally it is recommended to create annotated tags so you have all the information. Lightweight tags can be used for temporary purposes.

Create a tag for a commit

Create a tag for HEAD or a specific commit.

# Create a lightweight tag "v1.0" for HEAD.
$ git tag v1.0

# Create an annotated tag for HEAD with "-a" option.
# -a, this is an annotated tag which can include other information in an object,
#     such as message, name, email
$ git tag -a v1.0 -m "First formal version"

# Create a tag for commit 06d49a9
$ git tag v1.0 06d49a9

Note: By default, git push does not push tags to the remote repository, you need to explicitly push them:

# Push a tag "v1.0" to remote repository
$ git push v1.0

# Push all tags to remote repository
$ git push --tags

List tags

List all tags

# List all tags
$ git tag

List tags with specific patterns

# List all the tags that matching a pattern
$ git tag -l "v1.0*"
v1.0
v1.0.1

List tags with messages

# -n<num>, <num> specifies how many lines from the annotation. -n<mum> implies --list.
# If no number is given to -n, only the first line is printed.
$ git tag -n v1.0*
v1.0.2          Feature2
v1.0.1          Feature 1
v1.0            First formal version

Delete a tag

To delete a tag, use below command:

# Delete a tag
# -d, delete a tag with its name
$ git tag -d <tagname>

Examples

$ git tag -d v1.0

Resources