In this blog, we will try to learn about the tagging that we use in git. Git is a distributed version control system for tracking changes in any set of files. This was originally designed for coordinating work among programmers cooperating on source code during software development.
Going forward with this blog we will try to learn how to list existing tags, how to create and delete tags, and what the different types of tags are there.
What is tagging?
Long story short, like any other VCS, git also has the ability to mark some important and specific points in the repository timeline or chronology with some tags. This marking is called tagging in VCS’s terminology. Generally, developers use this functionality to mark releases with tags such as v1.0 , v2.o and so on.
Listing the existing tags.
This is pretty straight forward. We just have to hit the “git tags” command with optional “-l” or “–list” in our terminals and we are all done for listing the tags.
$ git tag v1.0 v2.0
This command will list all of the tags that are there according to the alphabetic order. If there are a lot of tags in our source repo, the tagging feature of git enables us to search the tags having some specific pattern.
Suppose, we need the tags of 1.8.5 series then we can add “v1.8” in our “git tag” command.
$ git tag -l "v1.8.5*" v1.8.5 v1.8.5-rc0 v1.8.5-rc1 v1.8.5-rc2 v1.8.5-rc3 v184.108.40.206 v220.127.116.11 v18.104.22.168 v22.214.171.124 v126.96.36.199
One important note here is that -l or –list is optional but when we are using the “git tag” command for searching a specific pattern then the use of -l or –list becomes mandatory.
Types of tags and how to create them?
Basically, in git, we have two types of tags.
- Light-weight tags
- Annotated tags
Let’s see what they are and how we can create them.
A lightweight tag is very much like a branch that doesn’t change. It is basically like a commit checksum that has been stored in a file no other information is kept along not even the name and email of the tag creator/tagger we can only see the details of the commit.
Unlike Annotated tags we don’t have to use -m, -s or -a while creating these types of tags, we just have to provide the name of the tag. For creating this type of tag we can use the command as given below. Please note that the v1.4-lw is the tag name.
$ git tag v1.4-lw $ git tag v0.1 v1.3 v1.4 v1.4-lw v1.5
Now, if you run git show on the tag that we just created, we would only see the commit, no extra information would be shown.
$ git show v1.4-lw commit ca82a6dff817ec4342007202690a93763949 Author: Albert Someone <email@example.com> Date: Sun Dec 27 21:52:11 2020 -0700 Change version number
Moving on to the next type of tag.
Annotated tags are somewhat similar to lightweight tags with a major difference, here we can have the information of the tag what we are going to create. For example, tagger name, email, date and the tagging message as well.
According to the best practises we should go for annotated tags instead of lightweight tags. If we want a temporary tag or for some reason, we don’t want to keep the other information, we should go for lightweight.
Creating an annotated tag is quite simple and very similar to the process of creating a lightweight tag. We just have to specify
-a when you run the
$ git tag -a v1.4 -m "my version 1.4" $ git tag v0.1 v1.3 v1.4
-m here signifies the tagging message here. If you do not specify the tag message git automatically launches our editor so that you can type in.
If you run the git show on the tag that we have just created we can see all information related to the tag.
$ git show v1.4 tag v1.4 Tagger: Albert Someone <firstname.lastname@example.org> Date: Sun Dec 27 23:52:11 2020 -0700 my version 1.4 commit ca82a6dff817ec4342007202690a93763949 Author: Albert Someone <email@example.com> Date: Sun Dec 27 21:52:11 2020 -0700 Change version number
This shows the tagger information, the date the commit was tagged, and the annotation message before showing the commit information.
We can delete the tag that we have created by using the “-d <tagName>” along with the “git tag” command. For example
$ git tag -d v1.4-lw Deleted tag 'v1.4-lw' (was e7d5add)
Remember, we have removed it from our local system, we would have to push these changes as well. We can do so by using the command,
$ git push origin --delete <tagname>
Thanks! I hope this blog could be of some help.