Updated April 13, 2023
Introduction to Git Tag
Before getting into the details of the Git Checkout Tag, let me give you a brief introduction to Git and why it’s so popular and useful. Git is a tool for distributed control used by product managers, developers, and data scientists to manage the program’s source code development and history. This article will get to know more about Git’s concept of tagging and how and when the git tag command is used.
What is Git Tag?
Tags are references that point to some particular points in Git history. It is mainly used to snapshot a particular point in the past and mark the release version (e.g. v0.0.1). It’s like a branch that does not change. They also do not have an additional history of commits. Let’s start by learning how to create new tags.
Creating New Tags
For creating a new tag, you can execute the following command:
git tag <tag_name>
To create a new tag, replace <tag_name> with a syntactically similar identifier that identifies the repository point when creating the tag. A common approach is using version numbers like git tag v2.5. Git has mainly two kinds of tags – lightweight tags and annotated tags. The above example was of a lightweight tag. Annotated tags and Lightweight tags are different with respect to the total amount of metadata they can store with the prior one storing more data consisting of email, date and tag name. The former tags are public, whereas the latter ones are private. Lightweight tags are just like ‘bookmarks’ to commit, basically a name that points to a commit and therefore can be useful to create quick links for related commits.
The commands to create a lightweight tag and an annotated tag are respectively:
git tag <tag_name>
git tag -a <tag_name>
Listing Tags
For listing the stored tags in a repo, the following command can be used:
git tag
This gives the list of tags as the output:
v1.12.0
v1.12.0-rc1
v0.13.0
v1.13.0-rc1
v0.13.1
v2.14.0
v0.14.0-rc1
v1.14.2
v0.12.0
v0.12.0-rc1
v1.12.0-rc2
To get a specific list of tags -l can be passed to the command along with a wild card expression:
git tag -l *-RC*
v0.12.0-rc1
v1.13.0-rc1
v0.14.0-rc1
v2.14.0-rc2
v0.15.0-rc1
v1.10.0-rc1
v14.0.0-rc.2
v14.5.0-rc.3
The above example shows the use of the -l option and a wild card expression of -RC that returns a list of all the tags with the specifications given pattern marked with that prefix, earlier used to recognize release candidates.
Checkout Tag
Say you have a project and you want to tag particular points on it. To checkout a tag, it should be locally present in your repository. For that, you have to fetch all the tags to your local repository.
git fetch –all
or
git fetch --all --tags –prune
After fetching all the tags, you can check out a tag using the command.
git tag -a <tag_version_name> -m
If you would like to go that t after some time, you have to first commit your current changes to ensure that you are free to check out new activities without losing the previous work. This is done by using:
git checkout tags/<tag_name>
You can also simultaneously create a new branch while you check out this tag, such that the current branch is not overwritten. The given below command is used for that.
git checkout tags/<tag_name> -b <branch_name>
To exit the current branch, you can go back to another branch by issuing this command.
git checkout <another_branch_name>
Notice that you only have to give that branch’s name for switching to a different branch, unlike with tags in which you have to insert the prefix ‘tags/’.
The command git checkout can be used to see the state of a repository as shown below:
git checkout v1.4
The above-mentioned command will check out the v1.4 tag by putting the repository in an unattached or uncoupled HEAD, the state, which means that none of the changes made will update the tag, thus creating a new detached commit. Now, this newly detached commit will not be a part of any of the previous branches and hence can only be reached directly by the commits. This tells us that, it is an excellent practice to spawn an entirely new branch whenever you want to make changes in a disconnected HEAD state.
If in a sample you have 2 tags say version 1.0 and version 1.1, then you can check out them executing any of the following commands:
git checkout B ...
git checkout version 1.1 ...
git checkout tags/version 1.1 ...
All of the above-mentioned commands will do the same thing as a tag is just a pointer to a given commit.
Deleting Tags
As the name suggests, deleting tags is used to delete a specified tag and can be easily done by using the below-mentioned command.
git tag -d <tag_name>
Bypassing the -d option to git tag along with the tag name that is to be deleted, you can delete the identified tag.
git tag
v1
v2
v3
git tag -d v1
git tag
v2
v3
In the given example, the git tag is first used to display the list of tags which are v1, v2 and v3; then, the delete command is executed to delete the v1 tag. This removes the deleted tag from the server.
Advantages of Git Checkout Tag
- It is used to create, modify and delete tags.
- It can be used to list all the tags in the local repository.
- It also helps to checkout remote branches.
- It helps to manage and handle releases.
- Keeps the repository & process clean and readable.
Conclusion
Git has many uses and is extensively used by developers, product managers, and data scientists. Its commands are very effective and can be very useful. To summarize, tagging is an additional mechanism that is used to capture the history of a Git repo. It is traditionally used to make semantic identifier tags that correspond to software release versions, but it is mainly used to create, modify and delete tags.
Recommended Articles
We hope that this EDUCBA information on “Git Checkout Tag” was beneficial to you. You can view EDUCBA’s recommended articles for more information.