Last modified: January 27, 2025

This article is written in: 🇺🇸

Creating and Managing Git Tags

Tags in Git provide a convenient way to reference specific points in your repository’s history. They are often used to mark important milestones, such as release versions (e.g., v1.0, v2.0). Unlike branches, which continue to move forward as new commits are added, tags are static references tied to a particular commit.

Creating Tags

Lightweight Tags

Lightweight tags are essentially just pointers to a commit. They contain only the tag’s name and the commit reference. These are quick to create and are often used for internal or temporary tagging.

Creating a Lightweight Tag for a Specific Commit

I. Get the commit hash (for example, b4d373k8990g2b5de30a37bn843b2f51fks2b40).

II. Run:

git tag <tag-name> <commit-hash>

Example

If you want to create a tag named v2.0 that points to the commit with hash b4d373k8990g2b5de30a37bn843b2f51fks2b40, you would run:

git tag v2.0 b4d373k8990g2b5de30a37bn843b2f51fks2b40

Creating a Lightweight Tag Using a Commit Reference

You don’t always need the raw commit hash. You can use a branch name or even a commit message (if it’s unique enough to be recognized):

git tag <tag-name> <branch-name>
git tag <tag-name> "<commit-message>"

Examples

Tag the latest commit on the master branch with v2.0:

git tag v2.0 master

Tag a commit with the message "Fix bug in login form" with v2.0:

git tag v2.0 "Fix bug in login form"

Annotated Tags

Annotated tags store additional metadata, including the tagger’s name, email, date, and a message. They are recommended when you want to keep a more descriptive record of why the tag was created or who created it.

To create an annotated tag, use the -a flag and include a message (-m):

git tag -a <tag-name> -m "<tag-message>"

Example

git tag -a v2.0 -m "Release version 2.0 with major feature updates"

When you run the above command, Git creates an annotated tag named v2.0. You can later use git show v2.0 to view the commit, tag message, tagger info, and date.

Viewing Tags

To list all tags in your repository, simply run:

git tag

You’ll get a list of all tags:

v1.0
v1.1
v2.0
...

To view more information about a specific tag, including the commit details and the annotated tag message (if it’s an annotated tag), use:

git show <tag-name>

Example

git show v2.0

This displays the commit at which v2.0 is pointing, the tagger’s name, email, date, and any message associated with the tag.

Pushing Tags to a Remote Repository

Pushing All Tags

By default, regular git push commands do not push tags. To push all tags from your local repository to the remote, use:

git push --tags

This will synchronize your local tags with the remote repository, allowing other collaborators to access them.

Pushing a Specific Tag

If you only want to push a particular tag:

git push origin <tag-name>

Replace <tag-name> with the exact name of the tag you wish to push.

Tags vs Branches

While both tags and branches are references to commits, they serve different purposes:

I. Mutability

II. Use Cases

In other words, if you want to mark a precise moment in history—like a deployed release—you use a tag. If you want to develop or maintain code over time, you use a branch.

Good Practices for Tagging

Table of Contents

    Creating and Managing Git Tags
    1. Creating Tags
      1. Lightweight Tags
      2. Creating a Lightweight Tag for a Specific Commit
      3. Creating a Lightweight Tag Using a Commit Reference
      4. Annotated Tags
    2. Viewing Tags
    3. Pushing Tags to a Remote Repository
      1. Pushing All Tags
      2. Pushing a Specific Tag
    4. Tags vs Branches
    5. Good Practices for Tagging