DEV Community

Cover image for Git Tagging Tutorial
Raunak Ramakrishnan
Raunak Ramakrishnan

Posted on

Git Checkout Tag Git Tagging Tutorial

I read a post on dev.to which shows how to create git tags using GUI-based git clients. I think that tags are useful to know even when using the git cli.

What are tags

Tags are specific points in your code history which are useful to re-visit later e.g
you just released a new version of your app. You can tag the commit as v1.0 using git tag v1.0. Anytime you want to reproduce bugs encountered on that version, simply do git checkout v1.0 and investigate.

How to use git tag better

Checkout code to the tag

The tag is linked to the specific commit and not to a branch. When you checkout the tag, git tells you that you are in "detached HEAD" state. Do not worry, all it means is that you need to create a new branch if you want to retain any changes you make after checking out the tag.

Create a new branch exactly at the commit of the tag using git checkout -b BRANCH_NAME TAG_NAME

Make your tag more informational!

You can add more information using git tag -a TAG_NAME -m 'MESSAGE'
The tag information can be viewed without having to checkout the tag using git show TAG_NAME.

Create tag at a particular commit

You do not always have to be at the HEAD or in the tip of the branch to create a tag. If you want to create tag say 5 commits before HEAD, you can use git log to get the correct commit hash e.g git log --pretty=oneline -10 which shows the last 10 commits on the current branch.

Then, create tag using git tag -a TAG_NAME -m 'MESSAGE' COMMIT_HASH

List and Delete tags

Listing all tags is simply: git tag
Similarly, delete a tag using git tag -d TAG_NAME

Share your tag with others!

The tag created is not pushed to remote automatically. If you want your tags to be used by other contributors too, you need to push them using git push origin TAG_NAME

Sign your tags!

If you are working on a major project and want to show without any doubt that you have worked on the release, you can sign it using your GPG private key as git tag -s TAG_NAME -m 'MESSAGE'. Anyone who runs git show TAG_NAME on the tag will also see your public key signature along with the tag information.

They can additionally verify the tag using git tag -v TAG_NAME. This checks using your public key whether the signature is indeed yours.

Tags vs Branches

Why use tags when you have branches? Because branches can change and tags are linked to a specific commit. Thus, marking releases with a tag will give you the state of code when the particular software release was done.

Summary

  • Tags are great way to remember specific commits
  • It is good to add a message to the tag so that people can easily see why you tagged a particular commit

References

The Git book chapters:

Top comments (1)

Collapse
 
darksmile92 profile image
Robin Kretzschmar

Great post!

I really need to use tags more often...