Git Pro Tip: How to List Tags that Contain a Commit with Git

As an iOS developer on an app that communicates with a Rails backend, I often ask the Rails team whether a change has made it to production. This Git Pro Tip answers the question: “Is [some commit] in production yet?”

Prerequisites: the code in question is controlled with git and every production release is tagged in a way that clearly indicates it’s in production. (If you’re not doing that, start doing it now. I can’t help you until you’re tagging your production releases. Make sure to make your tags readable and descriptive, like prod-2012-06-11.) We’re going to use git to list all the tags for a commit to answer our question.

Now let’s get back to your question: “Did [some fix] make it to production yet?” I’ll use the commit SHA f011235 for this example – I happen to know that f011235 is the commit I need in production, thanks to a descriptive commit message (like “fixed #811”). So I want to know if f011235 in production yet. We can use git tag --contains <commit> to list all the tags that contain a commit. Since I’m tagging all my production releases as described above, I can just do this:

$ git tag --contains f011235 | grep prod

And bam! Here’s my answer:

prod-2012-06-11

Thanks to a descriptive tagging scheme, the output tells me that the change was installed in prod today, 2012-06-11. So the answer to my question: “Did [some fix] make it to production yet?” is, indeed, “Yes.” If the change has been in prod for a while, you may see more results here (one for each production release). If the command returns no results, it’s not in production.

The exception to this rule is when you cherry-picked the change from a branch. To check whether you’ve cherry-picked the change, run git cherry like so:

$ git cherry -v f011235

If the commit has not been cherry-picked, you won’t see any output and you can go home knowing that the fix is not in production. If the commit has been cherry-picked, you’ll see another commit in the output:

- 193d6132b6f445b350baaa6bd43c560cc8ee2c82 fixed #811

You can then use this commit in the git tag --contains command to see if the change is in production:

$ git tag --contains 193d613 | grep prod

And just like before, you’ll see at least one tag in the output if the change is in production; if you don’t see any, it’s not in production.

You may have already noticed that since the git tag --contains <commit> command lists the tags that contain a commit, it can also answer the question, “Where is this change installed?” Just drop the grep from the above command and run like this:

$ git tag --contains f011235

And you’ll get a list of all the tags that contain that commit:

prod-2012-06-11
staging-2012-06-07
staging-2012-06-01

I’ve found git tags to be useful for knowing what’s in production, how long a change has been in production, and what I’ve done since the last release.

Learn to build crash-free apps in Swift by learning how to safely handle type casting, optionals, JSON parsing, and more in the 5-Part Guide to Getting Started to Swift by dropping your name and email in the boxes below.