Throughout the life of your project, you will reach various milestones. With Git, you 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. You can also use these tags as references to which you can reset your project.
But why use tags? Tags are one of the many different options you have in Git for providing additional context around improvements and modifications you make to your files over time, and they are relatively easy to use, understand, and integrate into your workflow.
If you are new to Git, be sure to check out our guide on learning Git.
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:
[email protected] $ git show v1.0 commit 0659595374f673bdffcc5a9d8b08efb145834132 Author: ChristopherM <[email protected]> Date: Wed Nov 1 10:13:25 2020 -0400 Improved database connection
You can see this output provides you with a decent amount of information including: the Git user, commit has, and date, as well as the commit message.
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:
[email protected] $ git tag -a v1.2 -m “my version 1.2“ [email protected] $ git show v1.2 tag v1.2 Tagger: ChristopherM <[email protected]> Date: Wed Nov 1 16:08:36 2017 -0400 my version 1.2 commit 0659595374f673bdffcc5a9d8b08efb145834132 Author: ChristopherM <[email protected]> Date: Wed Nov 1 10:13:25 2020 -0400 Added new test feature
Well done! You now know how to add tags with Git.
For more exhaustive detail, check out the complete documentation on Git tags.
Now That You Know All About Git Tags…
Check out these other resources from the Support Center: