Devops

How to create and push a tag using git

This minipost will guide you through all the required steps to create and push a git tag. Tags are really important in development as they mark specific versions of the developed software project.

In Rails development, tagging is falling under the Semantic Versioning 2.0.0, where three numbers separated with dots are used to define the version in a MAJOR.MINOR.PATCH fashion. We are not going to cover sem ver in this minipost, but we will demonstrate the git commands required to create a git tag.

Listing the existing git tags of a project

Before creating a new tag, you should list all the existing tags to identify the most recent tag.

To list all the tags within a project, run the following command:

$ git tag

Depending on the Semantic Versioning you are using, you will get an output of the following form:

v0.1.0
v0.1.1
v0.1.2
(END)

To terminate the terminal session, type: q

If you want to include the tag message in the same list, use the following command:

$ git tag -n

The above command will output:

v0.1.0      Version 0.1.0
v0.1.1      Version 0.1.1
v0.1.2      Version 0.1.2
(END)

To terminate the terminal session, type: q

Creating a new git tag

Now that you are aware of the version used at the latest tag, you could proceed by creating a new tag.

To create a new git tag, use the following command:

$ git tag -a v0.1.3 -m 'Version 0.1.3'

Now, verify that you have successfully created the desired version tag with the following command:

$ git tag -n

That will output:

v0.1.0      Version 0.1.0
v0.1.1      Version 0.1.1
v0.1.2      Version 0.1.2
v0.1.3      Version 0.1.3
(END)

To terminate the terminal session, type: q

Great, as you can see now, your newly created version is successfully created and it is the last one on the above list.

Pushing the new git tag to remote

Now that you have created a new version/tag for your project, you should push that tag to the remote.

Initially, you need to push any code that is locally:

$ git push origin master

Now, you should push the tags with the following command:

$ git push --tags

That's it, login into your remote and verify that the new tag appears on the tag list.

Additional information:

Buy Me A Coffee

Read also the following