Git tags are a way to mark specific points in Git history as important. They are refs that point to specific commits, and they don’t change over time (unlike branches, which are updated to point to new commits automatically as you create new ones). This article will look at some of the ways you can list git tags for your code repository, before also giving a few examples of how to create and how to delete git tags.
There are two main types of tags in Git: lightweight and annotated. Lightweight tags are simple refs that point to specific commits, while annotated tags are stored as full objects in the Git database. They contain additional metadata, such as the tagger’s name, email, and date, a tagging message, and can be signed and verified with GNU Privacy Guard (GPG).
To list the available tags in a Git repository, you can use the git tag
command. This will show you a list of all the tags in the repository, along with their commit hashes. For example:
$ git tag
v1.0
v1.1
v2.0
You can also use the -l
option to filter the list of tags by a specific pattern. For example, the following command will only show tags that start with the letter “v”:
$ git tag -l "v*"
v1.0
v1.1
v2.0
To list all tags in a repository, sorted by the date they were created:
$ git tag --sort=-creatordate
To list all tags that contain a specific keyword in their name:
$ git tag -l "*keyword*"
To list all tags that were created by a specific person:
$ git tag --contains --format="%(creatordate) %(refname)" | grep "John Smith"
To list all tags that were created on or after a specific date:
$ git tag --sort=creatordate --format="%(refname) %(creatordate)" | awk '/2022/{print; exit} {print}'
Create New Git Tags
To create a new Git tag, you can use the git tag
command followed by the name of the tag. For example:
$ git tag v3.0
This will create a new tag called “v3.0” at the current commit. By default, Git tags are not associated with any particular branch. This means that they will remain in place even if you switch branches or make new commits.
You can also create an annotated tag by using the -a
option. An annotated tag is a tag that contains additional metadata, such as the tagger’s name and email address, the date, and a message. To create an annotated tag, use the git tag
command followed by the -a
option and the name of the tag, and then add a message using the -m
option:
$ git tag -a v3.0 -m "Release v3.0"
View Git Tag Metadata
To view the metadata for an annotated tag, you can use the git show
command followed by the name of the tag. For example:
$ git show v3.0
tag v3.0
Tagger: John Smith <john@example.com>
Date: Mon Dec 18 14:14:21 2021 +0100
Release v3.0
commit abcdef01234567890abcdef01234567890abcdef01
Author: John Doe <john@example.com>
Date: Mon Dec 18 14:14:21 2021 +0100
Add new feature
How to Delete Git Tags
To delete a Git tag, use the git tag
command followed by the -d
option and the name of the tag. For example:
git tag -d v3.0
You can also delete multiple tags at once by providing a list of tag names separated by spaces.
Final Thoughts
As the examples in this article have shown, Git tags are useful for marking specific points in the Git history as important. They are similar to branches, but unlike branches, tags are not meant to be changed. This makes them ideal for marking specific versions of a project, such as releases.
Some of the main benefits of using Git tags include:
- Providing a way to mark specific commits as important: You can use tags to mark specific commits as important, such as releases, milestones, or important features. This allows you to easily identify and refer to these commits in the future.
- Keeping track of project versions: You can use tags to mark specific versions of your project. This can be especially useful if you are working on a project with multiple developers and need to keep track of which versions are in production, testing, or development.
- Simplifying deployment: You can use tags to mark specific versions of your project that are ready for deployment. This allows you to easily deploy the correct version of your project without having to manually check out the correct commit.
Overall, Git tags are a useful tool for marking specific points in the Git history and keeping track of project versions. They can help you manage your project more efficiently and streamline your workflow.