Git tags are one of those topics I’ve heard people discuss but I hadn’t had to use them much up until now, and one of the things I needed to understand better was how to list git tags, and see them within a list of my git commits. In this post I’ll explain how to show git tags so you can view them in a few different ways. First of all though, a bit of an overview of what git tags are, and why you should use them.
Table of contents
What is a Git tag?
Git has the ability to tag specific points in a repository’s history as being important. There are two types of tags in git – lightweight tags and annotated tags. A lightweight tag is just a simple ref stored under
.git/refs/tags. That means git commands can treat it like a tag.
How to tag the latest commit in Git
The simplest scenario would be if you wanted to tag the most recent commit on your current branch. To do so use the git tag command like this:
git tag v1.5
This will tag the latest commit as ‘v1.5’ on the current branch, and the format is
git tag <tag_name>.
How to tag a specific commit
But what if you want to tag a specific commit? You’ll need the hash of the commit so that you can refer to it when you tag it. And then you can use this command:
git tag <tag_name> <commit_id>
So if I were to use the following command, I would create a tag against the commit id 7560c10 called ‘v0.5‘ :
git tag v0.5 7560c10
SO now you’ve created a couple of tags, you want to know how to list them.
How to view Git tags
If you want to list all your tags, with no other information in a simple list simply use the
git tag command with no flags :
This will show you a simple list of the tags:
$ git tag v0.5 v1.0
However, it’s often more useful to view the tags against your list of commits, so for this you can use
git log, like this:
How to delete a tag
If you make a mistake when you’re tagging, you can delete a git tag using the
git tag command with the
-d flag like this:
git tag -d v1.5
… where v1.5 is the name of the tag you want to delete. In general terms you use the command like this:
git tag -d <tag_name>
I’ve given you an overview of what a git tag is, explained in basic terms how to create a git tag, how to delete a git tag and how to show your git tags.