Using the Git Tag Command

Git, like other VCSs has the ability to mark specific points in history as being important. You have have seen that when particular projects or applications are released, their version numbers may be present alongside the release notes (v1.0, v1.1 etc.). We can add relevant version numbers in Git as well. The following post will show you examples of using the git tag command.

To create a tag against the latest commit, you have to use git tag along with the -a option. This represents that you’re creating an annotated tag.

$ git tag -a v1.0

You should also specify a message using -m to provide a short description of the tag.

$ git tag -a v1.0 -m "Our first version"

As well as annotated tags, there are also lightweight tags. These differ from annotated tags as they act like a branch that doesn’t change, in that they’re just a pointer to a specific commit.

On the other hand annotated tags are stored as full objects in the Git database. It is generally recommended to use annotated tags as they have all the tag, date and other information stored against them.

Lightweight tags are created the same way, however without the -a, -s or -m options.

$ git tag v1.0

You can list the available tags in Git using the git tag command.

$ git tag

When using git push, the command will not transfer tags to remote servers. Therefore you will have to run the following:

$ git push origin v1.0

Not only does Git let you tag current commits, you can also tag commits you have already moved past. Let’s assume that your commit history looks like this:

$ git log --oneline
cd7f75f Changed the version number
ae85858 Removed unnecessary text
0acefc1 Initial commit

You could add a tag to the ‘Removed unnecessary text’ commit by specifying the short hash of the commit.

$ git tag -a v1.0 ae85858