How to tag an existing commit with a version

Ever seen the releases tab on github? Won­der how that’s cre­ated? Its done by cre­at­ing tags. Tags are ways to stick a label to a par­tic­u­lar git com­mit which you can revert to at any point of time. There are two kinds of tags — light­weight tags and anno­tated tags. Anno­tated tags are usu­ally more use­ful since they are not just labels but are a snap­shot of the code at the com­mit that you spec­ify and are there­fore just like a commit.

You can cre­ate an anno­tated tag for your head using the command

git tag -a tagName

This opens up a com­mit mes­sage dia­log before you can cre­ate the tag.

Or you can just use the fol­low­ing if all you need is a one line com­mit message.

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

Lets say that you have a branch with the fol­low­ing commits

A–B–C–D
where each alpah­bet rep­re­sents the com­mit check­sum and your head points to the lat­est com­mit — D.

Now if you want to tag a pre­vi­ous com­mit, say B with a tag­name v1.0 then you use the com­mand as follows

git tag -a v1.0 B

Note that tags don’t get pushed auto­mat­i­cally when you push a branch. You gotta do that explic­itly using the command

git push origin --tags

To list all the tags you have, you can run

git tag

Ryan Sukale

Ryan is just a regular guy next door trying to manage his life and finances.

You may also like...

Leave a Reply

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