Tagging

Once our project has reached a stable version, we will release it. That doesn't mean we'll never work on it again. A common and recommended practice is to bookmark the commit where a release happened. To do so we use a tag.


Now, suppose we want to create a release for our project right before the point in which we introduced app.py. We check the log and see that this means creating a tag at commit a642e12. To do so we run

git tag v1.0 a642e12

This will tell Git that we want to make a tag called v1.0 at commit a642e12. If we check the log now, we will see that Git has added that tag at that commit.


These type of tags are called lightweight tags because they are just pointers to a specific commit. A better way is to use annotated tags. These tags take a release message too.

Tagging 1
Tagging 2

First, let's delete the lightweight tag. To do this we use the -d option and supply the tag name, like so

git tag -d v1.0

You can check the log now and you'll see that the tag is not there anymore.

To create an annotated tag we use the -a option and supply it a message with the -m option

git tag -a v1.0 -m "Version 1.0" a642e12

Now let's create another tag at commit c674d68.

git tag -a v2.0 -m "Version 2.0" c674d68

Our log now shows that we have two tags pointing at different commits.


But v2.0 had a bug which we fixed in our last commit. So we'll release a new version of our program. The difference is that since we now only fixed a bug instead of introducing new functionality, we'll increment the tag minor instead of the major. Since we are releasing a tag at the last commit, we don't need to supply a commit ID

git tag -a v2.1 -m "Version 2.1"
Tagging 3
Tagging 4

To get a list of our tags we run

git tag -n

Once we have tags, we can checkout to those points in our commit history by name instead of using the commit ID. For example, running

git checkout v1.0

moves the HEAD to the v1.0 tag. Keep in mind that you are in a 'detached HEAD' state now, so you'll need to checkout to master again when done.