Using tags with Git to organize your commits

Brandon Wissmann
Jan 25 · 3 min read
Photo by Tim Gouw on Unsplash

Sometimes your commit history becomes a bit hectic. What you might not realize is that Git has a built in tagging system which allows you to “tag” certain commits you feel are important. This could either be a major release or anything else you see fit.

The git tag command will allow you to have a look at the existing tags of a project. For example, if we clone into the nuclear repo and run the aforementioned command we get this:

As we can see, this could get a bit disorganized and if we were to have dozens of tags could become nearly useless. Thankfully, you can also search for tags that contain a certain pattern.

This example allows us to only see points in the history that are tagged as version releases. Of course the pattern you search for could get as specific as you wish, using regular expressions.

git tag is not limited to searching for tags. The same command can be used to add tags to your own commits. The simplest way to accomplish this is by adding the -a flag to the command follow by the tag, an -m flag and the message you wish to associate with your tag. (Not supplying a message will cause git to prompt you for one). Let me illustrate this by adding my own tag to the nuclear project. Afterwards I will call git tag once again to illustrate the new addition:

As you can see, my tag has been added. To check the message of a tag we can simply enter the command git show . The show command allows you to see when the tag was added, the information of the person was tagged and the message they gave to the tag:

Now don’t worry if you are 5 months into your latest project and you haven’t tagged a single commit. It is very easy to go back in time and add tags to previous versions. To do this simply find the commit you wish to tag and at least the first few characters of its checksum. (use git log for this).

The -d flag will allow you to delete a tag in the same fashion. Now it is important to now that these tags are sent to a remote server (i.e. GitHub) by default. You have to push your tags like you would actual changes to files. In fact it tags an identical format: git push remote <tagname>. Now all your beautifully tags commits can be shared with the world! 🎆 By the way, you can use git checkout with tags just as you would do usually.

Before signing off I would like to touch a bit more on the git show command mentioned previously. git show is not only limited to tags. It can be used with blobs, trees, tags and commits. It is used like shown above, simply pass in the object name you wish to fetch information on.

I hope that this information proved useful to you and that I could be the Marie Kondo of your Git commit history. Feel free to reach out to me if there is some questions or suggestions you have! I’m always open to a good conversation.