Git Tags Cheatsheet

Introduction

What are git tags? You can think of it as a way to mark a point in the history of your git repo. They are like bookmarks that help you jump back and forth between versions of your code. At this point, you may say: But I can do all that with commit id. You are correct. However, git tags provide some values that make it a better choice in some cases, specifically:

  • Tags are easier to remember (since you can name them)
  • Annotated tags can include additional information such as release notes or other details. You cannot do this with a commit ID

Types of Git Tag

There are two types of tags at the time of this blog post:

Lightweight tags

Lightweight tags are simple pointers to a single commit to your repository. You can create them with just git tag

Annotated tags

If you need to provide more details to your tags, you can use annotated tags. For example, you want to include your email, name… in the tag, you should use annotated tags.

To create annotated tags, you can use git tag -a

Git Tag Cheatsheet

Let’s go over some of the most useful commands with git tag

Creating tags

# Creating a tag from the current commit
git tag <tagname>

# Creating an annotated tag with a message
git tag -a <tagname> -m "<message>"

# Creating an annotated tag at a specific commit
git tag -a <tagname> -m "<message>" <commit>

# Creating a lightweight tag at a specific commit
git tag <tagname> <commit>

Listing Tags

You can list your tags using these commands

# Listing all tags
git tag

# Listing tags that match a pattern
git tag -l "<pattern>"

View tags info

To view information regarding a tag, use git show

# Displaying tag information
git show <tagname>

# Displaying tag information for a specific commit
git show <commit>:<tagname>

Renaming and deleting tags

You can delete or rename a tag using the following commands

# Renaming a tag
git tag -f <tagname> <commit>

# Deleting a tag
git tag -d <tagname>

Push tags to remote

To push tags to remote, you can use these commands:

# Pushing a single tag
git push <remote> <tagname>

# Pushing all tags
git push --tags

# Deleting a remote tag
git push --delete <remote> <tagname>

Checking out tags

# Checking out a tag
git checkout <tagname>

# Checking out a tag and creating a branch
git checkout -b <branchname> <tagname>

# Checking out a specific commit based on a tag
git checkout <commit>:<tagname>

Tags best practices

Here are some best practices you can refer to when using git tags

  • Use annotated tags for releases
  • Use lightweight tags for bookmarks or temporary references
  • Use a consistent naming convention
  • Use tag prefixes to group related tags
  • Tag early and often

Conclusion

Git tags are a feature you don’t use as much as others (log, commit) but they are very powerful and useful tool to manage your repository, especially your releases. You can quickly view/jump to specific point in your repo using tags.

Leave a Comment