Like most Version Control Systems, Git has the ability to tag specific points in a repository’s history as important. Generally, people use this feature to mark version points (v1.0, v2.0, etc.). In this blog, you will learn about git tagging, how to list existing tags, how we can create and delete tags, and the different types of tags.
Listing of tags
- For listing all tags we have this command –
git tag (-l or –list)
Through this command, you will get the list of all tags present in that repository.
$git tag -l rel-5.1.1 rel-5.1.2 rel-6.1.1 rel-6.1.2 ...
- If you wish to apply filters on listing the tags, you can do it through the below command
git tag -l “rel-*”
This will give you all the tags that start with rel as a prefix.
$git tag -l "rel-6.*" rel-6.1.1 rel-6.1.2 rel-6.1.3 .....
Git supports two types of tags: lightweight tags and annotated tags. They both allow you to reference a specific commit in your repository, but they differ in the amount of metadata they can store.
Annotated tags store additional metadata as complete objects in the Git database, such as author name, release notes, tag message, and date.
git tag -a rel-5.2.1 -m “first tag of 5.2 release”
The – m Specifies the tag message, to be stored with the tag. If you don’t specify a message for an annotated tag, Git launches your editor so you can enter it.
If you execute the git show command you can see all the tag-related data.
$ git show rel-5.2.1 tag rel-5.2.1 Tagger: Alan Joe <firstname.lastname@example.org> Date: Fri Mar 09 18:56:52 2023 -0700 first tag of 5.2 release commit ca82a6dff81gg897662007202690a93763949 Author: Alan Joe <email@example.com> Date: Fri Mar 09 18:56:52 2023 -0700
Lightweight tags are the easiest way to add tags to a git repository, as they only store the hash of the commit they refer to. They are created without the -a, -s, or -m options, * containing no additional information.
You can create a new lightweight tag by executing the below command-
git tag v1.2.3
This time, if you run git show on a tag, you won’t see the additional info about the tag. This command only displays commits:
$ git show v1.2.3 commit ca596avpo89gg342007202690a93763949 Author: Matt Cole <firstname.lastname@example.org> Date: Fri Mar 09 18:56:52 2023 -0700
For deleting the tags on your repository you can use the below commands
For deleting the tag from the local repository you can run this command
git tag -d <tagname>
$ git tag -d rel-5.2.1 Deleted tag 'rel-5.2.1' (was e7d5add)
The above command will not delete the tag for the remote repo, for that you can use below two commands.
git push origin :refs/tags/<tagname>
git push origin –delete <tagname>
In this blog, we have learned about git tagging and how to create, list and delete tags. And also looked into the types of tags i.e lightweight and annotated tags. For more such blogs you can click here
Hope you enjoyed the blog. Thanks for reading.