Tags in Git

  • Last updated Apr 25, 2024

Git Tagging is a function of Git to mark a specific point as important in a repository's history. Generally, developers use tagging to mark release points such as v1.0, v2.0 and so on.

Listing Tags

You can list your existing tags using the command 'git tag' (with optional -l or --list):

git tag

You can also search tags using a specific pattern as shown in the example below:

git tag -l "v1.5.4"

Creating Tags

There are two type of tags in Git:

  1. Annotated Tag: Annotated tags are stored as objects in Git database. It contains information like tagger name, email, date, and a tagging message.
  2. The annotated tag can be created using the -a with the tag command. The -m specifies a tagging message. For example:

    git tag -a v1.0 -m "version release v1.0"

  3. Lightweight Tag: Lightweight tag is a pointer to a specific commit which is like a branch that does not change.
  4. The lightweight tag can be created by simply using the tag command without any options like -a -m. For example:

    git tag v1.0

It is generally recommended that you create annotated tags because it can contain information. However, you can also create lightweight tags if you want a temporary tag without any information.

Push Tag

After creating a tag, you may need to transfer the tag to the remote repository. To do this, you will need to push the tag to the remote repository. For example:

git push origin v1.0

If you want to push multiple tags at once, then you can use the --tags option with the git push command. For example:

git push origin --tags

Deleting Tags

Following is the syntax to delete a local tag:

git tag -d <tag-name>

This command does not delete the tag from the remote repository. To delete it from the remote repository, use the following command:

git push origin --delete <tag-name>

Checkout Tags

You can view the files that a tag may be pointing to by using the 'git checkout tag_name' command:

git checkout v1.0

This 'git checkout' command will put your repository to "detached HEAD" state which means any changes and commits will not change anything on that tag. To fix bugs on that tag, you will need to create a new branch by the exact commit hash and then work on the new branch.

git checkout -b version1 v1.0