Git Tag
Tag is a pointer to a specific commit in Git history. Generally, git tags are used to mark release points (v0.0.1, v1.0.0).
Tags are like branches that don't change. After a tag is created, it can't have more commits.
Git supports two different types of tags:
- Lightweight tag
- Annotated tag
Create a tag
Git provides a git tag
command to create tags. To create a tag, first checkout to the branch where you want to tag,
and run the command git tag <your_tag_name>
.
To create a tag, run the commands as follows:
$ git checkout new_branch
Switched to branch 'new_branch'
$ git tag v1.0.0
The above command will create a mark point on the new_branch branch using the name v1.0.0.
Annotated tags
An annotated tag is a named pointer to a commit with additional metadata. That metadata contains :
- The date of the tag
- The tag message
- The name and email of the person who created the tag
For security, annotated tags can be signed and verified with GNU Privacy Guard (GPG).
It's preferable and best practice for git tagging to choose annotated tags over lightweight tags.
The command to create an annotated tag will run as follows:
$ git tag -a v1.0.0
The command above will create a new annotated tag identified with v1.0.0, and the command will then open up the default text editor for more metadata input.
If you want to create an annotated tag without opening the default text editor, run the following command:
$ git tag -a v1.0.0 -m "my release version 1.0.0"
Lightweight tags
A lightweight tag is just a named reference to a commit without any further information about the tag.
The command to create a lightweight tag run as follows:
$ git tag v1.0.1
Listing tags
Git provides a command to list all stored tags in a repo. To display all tags, run the command as follows:
$ git tag
v1.0.0 v1.0.1 v1.1.1 v2.0.0 v2.0.0-rc1 v2.0.0-rc2
You can filter the list of tags using the -l option that can be passed with a wild card expression:
$ git tag -l *rc*
v2.0.0-rc1 v2.0.0-rc2
The above command returns a list of all tags marked with (rc) prefix. RC prefix is generally used to name release candidates.
Displaying tag details
Git offers a git show
command to get more information about a specific. The command run as follows :
$ git show v1.0.0
Tagging old commits
By default, the git tag
command will create a tag on the commit that HEAD is pointing to.
Also, Git can tag an old commit just bypassing the reference of a specific commit to the git tag
command.
To view a list of old commits, you can run the git log
command as follows:
$ git log --pretty=oneline
The above command will output a list of commits. And you can choose the one that you want to create from it.
The command to create a tag from a specific commit will run as follows:
$ git tag -a v0.1.1 6b5548fa8f6f80259cd45d211624843ebb3375ba
Running the above command will create a new annotated tag named v0.1.1 for the chosen commit.
Retagging old tags
Tags identifier are unique, so if you try to use the same tag identifier to reference more than a commit, git will throw an error telling you that the tag 'v1.0.0' already exists.
In the case that you want to update an already existing tag, the -f
FORCE must be used but with precaution as follows:
$ git tag -a v1.0.0 4b3251c5e09390ede938a57e103afc663ee538d6
The command above will update the v1.0.0 tag identifier to a new commit reference.
Pushing tags to remote
By default, git push
command will not push tags. Tags should be explicitly passed to git push.
The command to push a tag to a remote repo will run as follows:
Syntax :
$ git push <remote_repo_name> <tag_name>
Example:
$ git push origin v1.0.0
The command above pushes the tag identified by v1.0.0 to the origin remote repo.
You can also push all the tags to the remote repo using the --tags
option as follows:
$ git push --tags origin
Checking out tags
There is no practical use in checking out tas in Git. Instead, we can check out a tag by creating a new branch. the command will run as follows:
$ git checkout -b new_branch_v1.0.0 v1.0.1
Deleting tags
You can delete a tag just by passing -d
option and a tag identifier to the git tag
command.
The command to delete a tag will run as follows:
$ git tag -d v1.0.0
If you want to delete multiple tags at the same time, run the below command:
Syntax :
$ git tag -d <tag_name_1> <tag_name_2>