How to add Tag in Git Repo using best practice

In this artilce we will learn how to add tag in git repo using best practice and example. It is best feature in git which also allows users to tag specific version of the repository with meaningful names.

For example, you have completed 1st version of your application source deplyment and you want to tag this point with specific notation.

Let’s take a example to see how tag is useful in git-

Creating Git Tags

It is known that Git supports two types of tags: lightweight and annotated.

1-What is lightweigthed Tags ?

“A lightweight tag is very much like a branch that doesn’t change — it’s just a pointer to your specific commit.”

2-What is Annotated tags ?

Annotaed tag are stored as full objects in the Git database. In database it contains information like tagger name, email, and date; have a tagging message; and can be signed and verified with GNU Privacy Guard (GPG).

It is best practice to create annoted tag so you can have all this information in databse.

Example-

a) Create Annotated Tags-

It very simple to create an annotated tag in Git. The easiest way is to specify -a when you run the tag command:

$ git tag -a v1.1 -m "version 1.1"
$ git tag   # List the existing tags
v1.0
v1.1

If you want to see full details of any exiting tag, use git show command:

git show v1.1
commit b3rs665g
Author: demo <[email protected]>
Date:   Sun July 17 2:52:11 2021 -0200

Change version number

Push your created tag to remote repository using the command:

git push origin tag v1.1 

Counting objects: 1, done.
Writing objects: 100% (1/1), 266 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://github.com/dummy/demorepo.git
 * [new tag]         v1.0 -> v1.0

b) Create Lightweight Tags

To create a lightweight tag, you need not to supply any of the -a-s, or -m options, just give a tag name:

git tag v1.4-lwt
$ git tag
v1.0
v1.1
v1.1-lwt

To see the tags-

git show v1.1-lwt
commit a2bsr39m
Author: demo <[email protected]>
Date:   Sun July 17 3:08:11 2021 -0200

Change version number

Now let’s assume that you forgot your project or application release version(tag name) and now you want to see existing tag voersion to apply an new version, then a simple command is useful-

git log ## find the commint ID
git tag -a "v1.1" -m "Release 1.1" 45ae651859729838c6c098d0029786d868b3d9d8
git push origin tag v1.1

Git tag useful commands

List Tags

You can list all the existing tags available in your repository.

git tag -l
v1.0
v1.1
v1.1-lwt

Checkout Tags

git checkout -b <barnch name> v1.1

Delete Tags

git tag -d v1.0

Deleted tag 'v1.0' (was 6c098d0)

I hope given information is useful for gitops. You can also check how to change remote path in git .

About Shachindra Yadav

I, Shachindra Yadav, a Cloud Certified, DevOps and technology enthusiastic person, more passionate to learn and write blogs in my free time.

View all posts by Shachindra Yadav →