Mohit Khare
Mohit Khare Product Engineer @ Gojek | Blogs about tech, productivity and life.

Git tags : Explained

Git tags : Explained

Git is a beautiful and quite necessary tool for developers. It eases our lives in maintaining codebases shared across multiple people with ease.

Using Git-scm simple definition here -

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

If you are not aware of git, i’ll recommend to learn basics first -

In this article, we are going to understand what, why, when and how’s about Git tags. This should be enough to get to started to use tags in your projects.

Let’s get started !!

What?

Tagging basically refers to having references to certain points ( i.e milestones) in Git history. Tags allow you to capture specific points in your code’s git history which can be used anytime later.

You can treat a tag like a branch that doesn’t change.

Tag represents a version of a particular branch at a moment in time. Thus, tags essentially lets developers mark important checkpoints in their development process.

Why and When?

You must have seen something like v1.0.0 . This generally denotes version/release number of a product. Generally, when some major changes are done and need to be shipped to users, a tag is created.

Tags are usually used to mark a certain milestone like a new feature releases or some bug fix which can be distributed to users/enterprises.

You don’t want this to change again and again. Thus, using tags ensures that the user gets the version which was created at a certain point of time irrespective of new commits added to the codebase.

How ?

Now that you understand the use case of tags, let’s see how we can actually use it. Time to implement tags ✅

Creating a tag

1
2
git tag <tag_name>
// Eg. git tag v1.0.1

This command tags the latest commit in your current branch. Here tag_name is basically a name which you want to give to this commit. It is generally something like v1.0.1 , although you can have it as anything which you want.

Generally the naming pattern followed is -

1
v<major>.<minor>.<patch>
  • major: breaking changes
  • minor: version compatible with previous version
  • patch: bug fix

Once you created a tag, you can push it to remote using -

1
git push --tags

You can also create a tag with message

1
2
git tag -a <tag_name> -m "message"
// Eg. git tag -a v1.0.1 -m "First release"

Wait, what is -a here? Let’s dig in!

Types of git tags

There are 2 types of git tags -

  • Annonated
  • Lightweight

Annonated Git tags

Annotated tags store extra metadata such as author name, release notes, tag-message, and date as full objects in the Git database. This data is important for a public release of your project.

The -a in the previous example is to make this tags annonated.

1
git tag -a v1.0.1 -m "First release"

Lightweight Git tags

Lightweight tags are the simplest way to add a tag. They just store the name and hash of the commit they refer to and they don’t contain any extra information. They act like simple bookmarks.

1
git tag v2.1-lw

lw signifies its a lightweight tag. Adding a lw is completely optional, it’s just for better readability.

Create git tag for specific commit

1
git tag <tag_name> <commit_sha>

In case, you want to create a tag for some older commit, this can be achieved with the above command.

Listing all tags

1
git tag

The above command lists down all the existing tags.

You’ll see output something like -

1
2
3
4
5
6
1.0.0
1.0.1
1.0.2
1.0.3
2.0.0
2.1.0

You can also use -l or --list option to filter out tags by using regular expression.

1
git tag -l "1.0*"

Results in -

1
2
3
1.0.0
1.0.1
1.0.2

Fetch tag details

1
2
git show <tag_name>
// Eg. git show v1.0.1

This shows the author, tag message, respective commit, date and other details.

Deleting a tag

1
git tag -d <tag_name>

Pushing tags

1
git push origin <branch> --tags

Use this command to push your tags to remote.

Pulling tags

1
git fetch --tags

Use this command to pull tags to your current branch from remote.

Checkout tags

1
2
git checkout <tag_name>
// Eg. git checkout v1.0.1

In case you want to go back to state of codebase at a certain tag, you can use the above command to checkout a particular tag

You have a good knowledge about git tags now, start using tags in your projects 😁

More Resources to learn


I hope you learned something interesting and new. 🎉

Don’t miss out on latest blogs - Subscribe now. ✅

Interested in more technology, productivity and life stuff? I share updates/knowledge almost daily on Twitter.

Reach out to me at mohitkhare.me

comments powered by Disqus