Tags

, , , , , , , ,

Tags are ref’s that point to specific points in Git history. Tagging is generally used to capture a point in history that is used for a marked version release (i.e. v1. 0.1). A tag is like a branch that doesn’t change. Unlike branches, tags, after being created, have no further history of commits. Tagging is an additional mechanism used to create a snapshot of a Git repo. Tagging is traditionally used to create semantic version number identifier tags that correspond to software release cycles. The git tag command is the primary driver of tag: creation, modification and deletion. There are two types of tags; annotated and lightweight. Annotated tags are generally the better practices as they store additional valuable metadata about the tag.

Tag Commands

To list stored tags in a repo execute the following

$ git tag

Search tags for a particular pattern:

$ git tag -l <tag-pattern>

Show a tag data:

$ git show <tag-name>

A common pattern is to use version numbers like git tag v1.4. Git supports two different types of tags, annotated and lightweight tags.

Annotated Tag:

Annotated tags are stored as full objects in the Git database. To reiterate, They store extra metadata such as the tagger name, email, and date. Similar to commits and commit messages Annotated tags to have a tagging message. Additionally, for security, annotated tags can be signed and verified with GNU Privacy Guard (GPG). Suggested best practices for git tagging is to prefer annotated tags over lightweight so you can have all the associated meta-data.

Create an Annotated Tag:

$ git tag -a <tag-name> -m <tag-message>

Lightweight Tag

Another way to tag commits is with a lightweight tag. This is basically the commit checksum stored in a file — no other information is kept. To create a lightweight tag, don’t supply any of the -a, -s, or -m options, just provide a tag name. Lightweight tags create a new tag checksum and store it in the .git/ directory of the project’s repo.

Create a Lightweight Tag

$ git tag <tag-name>

Create a tag for a specific commit:

$ git tag -a <tag-name> <commit-checksome>

Push a specific tag to remote:

$ git push origin <tag-name>

Push all the tags to remote:

$ git push origin --tags

Checkout a specific to local:

$ git checkout -b <branch-name> <tag-name>

Deleting tags is a straightforward operation. Passing the -d option and a tag identifier to a git tag will delete the identified tag.

$ git tag -d <tag-name>  [NOTE: Think multiple times before execution]

Happy learning!!