Let’s Play Git Tag! Why and How You Should be Tagging in Git

Article summary

Tagging in Git is not a new concept, and some readers might be thinking, “Why am I reading this article about Git tags?” But, for those who are curious or who have never used Git tags before, I will explain what tagging is and the best use of these tags.

Tagging in Git

Let’s start by talking about what tagging in Git is and how to go about it. To put it simply, Git tagging is creating a named marker on a specific commit of a repository. This can be on any commit for any branch that is currently part of the repo. This tag will come with a message or title to help identify it in the sea of tags.

This name could be anything you want. If you really felt like it you could name a tag “tag-your-it” and the next one “No-tag-your-it” and the following one “it-is-you’re-not-your.” Those tag names will now be tied to the commits you’re currently on when creating the tag. Also important to note is that tag names are unique, meaning no two tags will have the same name to avoid confusion.

So, that sounds great, but how do you actually tag a commit? The command is this:

Git tag <tag-name>

Simple right? Well, not exactly. Much like any other tool, there are some caveats. The first thing to understand is that Git tags are not branch-specific but repository-specific. That means if someone creates a tag, it will be available to everyone with access to the repository. You don’t get up-to-date tags when you do a Git pull. To get the latest tags, you must perform a Git fetch command.

Git fetch

With that command, you will have all the up-to-date tags pulled down into your remote repository. From here we can check out a tag using the checkout command. For example, using the previous example tags we can run this:

Git checkout tags/tag-your-it

This will check out that tag locally.

Using Git Tags

The best use I have found for tags is to use them to help with the releases of your app. Say you have multiple environments that you deploy to and you only want certain versions of the codebase in one environment. You can use tags to keep track of what version of the code should go into that environment.

With a Git tag you will have a way to know exactly what code to deploy down to the commit. The proper order for this would be to tag the commit using the command listed above, and then you will need to push that tag up to the origin with the command.

Git push origin <tag-name>

You can either manually deploy or set up your CI/CD pipeline to deploy anytime a push has a tag with it. This way you can have better and more granular control over what code is deployed where.

Git tagging is a great way to keep track of the state of your code for releases. I hope this clears up what tagging in Git is and how to use it.


Join the conversation

Your email address will not be published. Required fields are marked *