Git Tagging
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
v1.0
v2.0
v3.0
You can also search tags using a specific pattern as shown in the example below:
git tag -l "v1.5.4"
v1.5.4.1
v1.5.4.2
v1.5.4.3
v1.5.4.4
v1.5.4.5
v1.5.4.6
v1.5.4.7
Creating Tags
There are two type of tags in Git:
- Annotated Tag - annotated tags are stored as objects in Git database. It contains information like tagger name, email, date, and a tagging message.
- Lightweight Tag - lightweight tag is a pointer to a specific commit which is like a branch that does not change.
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.
Annotated Tag
The annotated tag can be created using the -a with the tag command. The -m speficies a tagging message.
Example
git tag -a v1.0 -m "version release v1.0"
You can use the git show command to see the tag information.
Example
git show v1.0
tag v1.0
Tagger: jake <[email protected]>
Date: Thu Dec 17 13:40:50 2020 +0545
my version release v1.0
commit 46c763f2441dc84359cfdc6df8a233db1f5f7f23 (tag:v1.0)
Author: jake <[email protected]>
Date: Thu Dec 17 13:37:11 2020 +0545
Lightweight Tag
The lightweight tag can be created by simply using the tag command without any options like -a -m.
Example
git tag v1.0
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.
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.
Example
git push origin --tags
Deleting Tags
Following is the syntax to delete a local tag:
git tag -d <tag-name>
Example
git tag -d v1.0
This 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>
Example
git push origin --delete v1.0
Checkout Tags
You can view the files that a tag may be pointing to by using the git checkout <tag-name> command.
Example
git checkout v1.0
This 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.
Example
git checkout -b version1 v1.0