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 not only used by product managers and developers but also data scientists to manage the source code development of the program and its history. In this article, we’ll 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 to mark the version of the release (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 to the point of the repository 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>
For listing the stored tags in a repo the following command can be used:
4.5 (2,150 ratings)
This gives the list of tags as the output:
To get a specific list of tags -l can be passed to the command along with a wild card expression:
git tag -l *-RC*
The above example shows the use of -l option and a wild card expression of -RC that returns a list all the tags with the specifications given pattern marked with that prefix, earlier used to recognize release candidates.
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
And if after some time, you would like to go that tag, 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 for switching to a different branch you only have to give that branch’s name, 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.
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 -d v1
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 form 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 Checkout Tag
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.
This is a guide to the Git Checkout Tag. Here we discuss how to Create new Tags and Check out Tags along with its advantages. You can also go through our other suggested articles to learn more–