How to tag a commit in git?

The git commit is a 40-digit hexadecimal SHA1 hash. Quite often we need to bookmark a as the commit hash is difficult to memorize. This is where one can use tags. Tags can be used to name a commit. In other words, tags are labels that can be used to identify a specific commit. For e.g., “v1.0, RC1.0” are some ways to name a commit.
Tags can be classified as −
Lightweight Tags
Lightweight Tags
Annotated Tags
Annotated Tags
A Lightweight tag is also known as a simple tag. These tags use a name to refer to a specific commit. Lightweight tags are private to a repository. These are just pointers to a specific commit. In other words, they only store the hash of the commit they refer to and do not store any information. A lightweight tag is not stored as a separate object in Git.
A tag can be created using the git tag command. The syntax for using this command is −
$ git tag <tag_name> <commit_hash>
The following example creates a tag “RC1.0” and associates it with a commit having the hash “c81c9ab”
$ git tag RC1.0 c81c9ab
Let us verify this by executing the following commands −
$ git tag             // lists all tags
$ git log −−oneline   // lists all commits, one commit per line
The following output indicates that a tag by the name “RC1.0” exists in the repository and it points to the commit “c81c9ab”.
089ddf4 (HEAD −> master) new line
c81c9ab (tag: RC1.0) This is a short description
8a3d6ed first commit
When we create a lightweight tag, a file with the name of the tag will be created inside the “.git/refs/tags folder”. This can be verified by executing the following commands −
$ ls .git/refs/tags
The output will be −
RC1.0
$ cat .git/refs/tags/RC1.0
The output shows that a tag “RC1.0” is created and that it points to a specific commit.
c81c9abe7c4c8273a11ab7d20a3aac0be54bf00c
An annotated tag stores extra metadata such as author name, release notes, tag-message and date. Unlike lightweight tags, annotated tags are stored as a separate object in Git. These tags allow you to store information that is related to this specific tag. An annotated tag has a tagging message just like a commit has a commit message. The syntax to create an annotated tag is −
$ git tag −a <tag_name> −m <tag_message>
The −a specifies that the tag being created is an annotated tag. The −m flag denotes a tag message which is similar to commit message.
The following example creates an annotated tag v1.1 and adds a message to it.
$ git tag −a v1.1 −m 'my version 1.1'

Many Git commands work only on annotated tags, because they are considered permanent objects. Annotated tags are stored as tag−type objects, that point to a commit. We can verify this by executing the following commands −
$ ls .git/refs/tags
The following output shows that “.git/refs/tags folder” has our “v1.1” tag file.
RC1.0 v1.1
When we display contents using the cat command it shows a hash value.
$ cat .git/refs/tags/v1.1 //view content of the tag file
The output is
fa2c7b87859db191440e97ec87e47b212c44945d

Let’s verify the type of this hash using the following commands.
$ git cat−file −t fa2c   // shows type of the object represented by this hash
$ git cat−file −p fa2c   // prints the contents of the SHA1 hash
The output of the above commands will be −
// output of git cat−file −t fa2c
tag
// output of git cat−file −p fa2c
object 089ddf457ded2496a5ad5ac4384a93a109b9b7e2
type commit
tag v1.1
tagger Kiran <kiran.p@gmail.com>1612112082
The output shows the “fa2c” object which is contained in the “v1.1” tag file is a tag object. When we display details of the “fa2c” blob object using the cat−file −p command it points to commit “089dd” which is the last commit. We can also see the tagger name and tag message details.