• 0

How to tag an existing commit with a version

Ever seen the releases tab on github? Wonder how that’s created? Its done by creating tags. Tags are ways to stick a label to a particular git commit which you can revert to at any point of time. There are two kinds of tags - lightweight tags and annotated tags. Annotated tags are usually more useful since they are not just labels but are a snapshot of the code at the commit that you specify and are therefore just like a commit.

You can create an annotated tag for your head using the command

git tag -a tagName

This opens up a commit message dialog before you can create the tag.

Or you can just use the following if all you need is a one line commit message.

git tag -a tagName -m "My commit message"

Lets say that you have a branch with the following commits

A–B–C–D
where each alpahbet represents the commit checksum and your head points to the latest commit - D.

Now if you want to tag a previous commit, say B with a tagname v1.0 then you use the command as follows

git tag -a v1.0 B

Note that tags don’t get pushed automatically when you push a branch. You gotta do that explicitly using the command

git push origin --tags

To list all the tags you have, you can run

git tag