Git tag is used to mark a specific point in the git history. And are mostly used for creating project releases.
Git tags are similar to git branches but no change can be made once a tag is created.
In this tutorial, you will learn about how to create new git tags for your project.
Create a new git tag
The simplest and most straightforward way to create a new tag is by running the “git tag” command with the tag name.
$ git tag <tag_name>
The above syntax can be used to create a tag by replacing <tagname> with the actual tag name.
Here is an example of how someone might use this git command in real life to create a tag.
$ git tag v1.0
In the above example “v1.0” is the name of your new git tag.
Create a git tag from a commit
Git tag can also be created from a particular commit SHA from git history. You can use the “git tag” command with the tag name and commit SHA for which tag need is created.
Syntax to create git tag from a commit:
$ git tag <tag_name> <commit_sha>
Example to create a git tag from a commit:
$ git tag v1.0 c69d03e
If you want to create a tag from the last commit then you can simply use a HEAD option as shown below.
$ git tag v1.0 HEAD
Git tag can also be created from the commit SHA with an annotation tag by adding “-a” and “-m” options.
$ git tag -a <tag_name> <commit_sha> -m "Your message"
View available git tags
Once the tag is created you can just run the “git tag” command to confirm the tag and get all the tag you have available.
$ git tag
-l option can be added to "git tag" command to further refine the result if you have a big list of tags. For example, if you only want to see tags for v2.0 and sub-releases.
$ git tag -l "v2.*"
Type of git tags
As now we know what are git tags and why we need them, let’s talk about the type of tags git supports. You can have two types of tags in git, one is Lightweight Tags and another is Annotated Tags.
As the name suggests Lightweight Tags is the simpler and trimmed down version of creating a tag without any meta-information about the tag. The example you have seen above is the example of Lightweight Tags, and here is the syntax again.
$ git tag <tag_name>
Annotation Tags are a method of creating a git tag with some extra meta information. To create an annotation tag you just need to add “-a” with the git tag command and “-m” to specify the message.
$ git tag -a v1.0 -m “Release v1.0 create.”
And now you can use the “git show” command to see all the data attached with the tag we just created with annotation.
$ git show v1.0
Tagger: John Corner <[email protected]>
Date: Sun Oct 25 16:41:00 2020 +0800
Release v1.0 created.
(HEAD -> master, tag: v1.0, origin/master)
Merge: 83633c99e 0714ce67b
Author: John Corner <[email protected]>
Date: Sun Oct 18 04:42:24 2020 +0000
So in this blog post, we learned about how to create a git tag for your project release with examples.
We also covered how to create a git tag from a commit SHA and how to view the created git tags. Later we cover Lightweight tags and Annotation tags with examples.
You can read further about git tags in the following articles: