How to Use Tags in Git

Throughout the life of your project, you will reach various milestones. With Git, we mark significant improvements and modifications with commits, but often there will be events that will require more annotation, such as version changes. In this article, you’ll learn about how to use tags in Git to mark significant events in the life of your project.

If you are new to Git, be sure to check out our introduction to Git and installation guide.

Before proceeding with tagging, we will want to highlight the different types of tags available. There are two main tag types: lightweight and annotated.

For a quick tag, merely a reference to a certain commit, the lightweight tag is sufficient. However, the annotated tag is recommended because it contains more detailed metadata that might be helpful to you later.

How to add a Lightweight Tag

Adding a lightweight requires only a quick and easy “tag” command with no options:

 git tag v1.0 

Now, a tag of “v1.0” will reference the most recent commit. To use this tag as a reference use this command:

 git show v1.0 

This output of this command will be a display of the commit referenced and changes that were made in that commit. The below command line output demonstrates this:

christopher@server $ git show v1.0
commit 0659595374f673bdffcc5a9d8b08efb145834132
Author: ChristopherM <chris@example.com>
Date: Wed Nov 1 10:13:25 2017 -0400

Menu position adjustment

How to Add an Annotated Tag

In order to add an annotated tag, you will use the same “tag” command. However, there are a few options to add in this case. Use the -a option to make this tag “annotated” and the -m command to provide the tag with a message (similar to a commit message).

 git tag -a v1.2 -m "my version 1.2" 

Then, to show the output of this tag’s reference, us the Git “show” command again, as in the below command line instance:

christopher@server $ git tag -a v1.2 -m “my version 1.2
christopher@server $ git show v1.2
tag v1.2
Tagger: ChristopherM <chris@example.com>
Date: Wed Nov 1 16:08:36 2017 -0400

my version 1.2

commit 0659595374f673bdffcc5a9d8b08efb145834132
Author: ChristopherM <chris@example.com>
Date: Wed Nov 1 10:13:25 2017 -0400

Added new test feature

Well done! You now know how to add tags with Git.

Was this article helpful? Let us know!