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.
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" v126.96.36.199 v188.8.131.52 v184.108.40.206 v220.127.116.11 v18.104.22.168 v22.214.171.124 v126.96.36.199
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.
The annotated tag can be created using the -a with the tag command. The -m speficies a tagging message.
git tag -a v1.0 -m "version release v1.0"
You can use the git show command to see the tag information.
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
The lightweight tag can be created by simply using the tag command without any options like -a -m.
git tag v1.0
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.
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.
git push origin --tags
Following is the syntax to delete a local tag:
git tag -d <tag-name>
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>
git push origin --delete v1.0
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 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