Adding Git Tags To Your Commit History
If your workflow does not have a dedicated “production” and “development” branch. You might find yourself with a master branch that has 50+ commits and a handful of those are “stable” and rollback worthy. Here is a quick little reference on how to get tags in place so you can rollback to a stable commit at a moments notice.
If you don’t know how to tag in git you can read this brief description on git tags. This post is about going back to your commit history and accurately placing your tags for future rollbacks.
Finding where to place your tags
All you need for a git tag is the SHA1 of the commit for which you wish to tag. The trick is: How do I get a list of my commits and how tedious is it to copy the SHA1 for every tag? I know of two ways to get a list of your commits and their SHA1s.
A.) Use the log command to show a list of all commits
This command will render a side by side of SHA1 and commit message. Note that for git tags you ONLY need the first 7 characters of the SHA1. No need to copy more than that manually.
git log --pretty=oneline
B.) Github is your friend
My method of choice is to use Github.com‘s interface to copy the SHA1s I need. Not only can I copy the SHA1 to my clipboard but I can easy see exactly where in my commit history I want to place my tag. Github.com‘s UI is slick and easy to use.
Placing your tags
The hard part is done. Whether you are using Github.com to copy your SHA1s to your clipboard or using the log command to write down the first 7 characters of the SHA1 you are good to go. Let’s suppose you want to create a tag named “v1.0″ at commit ec32d32.
git tag v1.0 ec32d32
Sometimes you make a mistake or you prematurely release a “stable” tag then realize it is far from stable. Let’s delete tag “v1.0″.
git tag -d v1.0
Pushing tags to a remote server
Tags do not get pushed by default to a remote. Also, if you created a lot of tags for your commit history you can easily push them all to a remote server.
git push origin --tags
You can easily place a handful of tags into the proper place in your commit history. If you have a nice method you use please share in the comments below.