What is git tag, How to create tags & How to checkout git remote tag(s)

Learn what is git tag, how to create tags & how to checkout git remote tag(s) with practical examples, diagrams, and best practices. Covers git, git-checkout, git-tag development techniques with vi...

Git Tags: Understanding, Creating, and Checking Out Remote Tags

Hero image for What is git tag, How to create tags & How to checkout git remote tag(s)

Learn what Git tags are, how to create them for marking significant points in your repository's history, and how to effectively checkout remote tags.

Git tags are essential tools for marking specific points in your repository's history as important. Typically, people use this functionality to mark release points (e.g., v1.0, v2.0). A tag is essentially a reference to a specific commit, much like a branch, but unlike branches, tags are not meant to move. Once created, a tag always points to the same commit. This article will guide you through understanding Git tags, creating both lightweight and annotated tags, and the process of checking out remote tags from a shared repository.

What are Git Tags?

Git provides two main types of tags: lightweight and annotated. Understanding the difference is crucial for effective version control.

Lightweight Tags A lightweight tag is simply a pointer to a specific commit. It's like a branch that never moves. They are useful for temporary marking or for private use where you don't need to store extra information.

Annotated Tags Annotated tags are full-fledged objects in the Git database. They are checksummed, contain the tagger's name, email, and date, have a tagging message, and can be signed with GPG. It's generally recommended to use annotated tags for public releases because they provide more metadata and security.

flowchart TD
    A[Start Commit] --> B(Commit 1)
    B --> C(Commit 2)
    C --> D(Commit 3)
    D -- "git tag v1.0" --> E[Lightweight Tag: v1.0]
    D -- "git tag -a v1.0.1 -m 'Release 1.0.1'" --> F[Annotated Tag: v1.0.1]
    E -.-> D
    F -.-> D

Illustration of lightweight vs. annotated tags pointing to a commit.

Creating Git Tags

Creating tags is straightforward. You can tag the commit you're currently on, or a specific commit in the past. Always consider whether a lightweight or annotated tag is more appropriate for your use case.

# Create a lightweight tag on the current commit
git tag v1.0

# Create an annotated tag on the current commit
git tag -a v1.0.1 -m "My first annotated tag for release 1.0.1"

# Create a lightweight tag on a specific commit (using its SHA-1 hash)
git tag v0.9 9fceb02

# Create an annotated tag on a specific commit
git tag -a v0.9.1 9fceb02 -m "Annotated tag for v0.9.1"

Commands to create lightweight and annotated Git tags.

Pushing Tags to Remote Repositories

By default, git push does not transfer tags to remote servers. You must explicitly push tags after you've created them locally. This ensures that your tags are shared with other collaborators.

# Push a single tag to the remote
git push origin v1.0

# Push all local tags to the remote
git push origin --tags

Commands to push local tags to a remote repository.

Checking Out Remote Git Tags

When you want to inspect the state of your project at a specific tagged release, you can 'checkout' the tag. This will put your repository in a 'detached HEAD' state, meaning you are not on any branch. You can look around, make experimental changes, and even commit, but those commits won't belong to any branch unless you explicitly create one.

1. Fetch all tags from the remote

Before checking out a remote tag, ensure your local repository has all the latest tags from the remote. This is done by fetching them.

2. List available tags

After fetching, you can list all available tags to find the one you want to checkout.

3. Checkout the desired tag

Use the git checkout command followed by the tag name. This will place your repository in a detached HEAD state.

If you plan to make changes or further development from this tagged point, it's best to create a new branch immediately after checking out the tag. This attaches your HEAD to a new branch, allowing normal development.

# Step 1: Fetch all remote tags
git fetch origin --tags

# Step 2: List available tags (optional)
git tag

# Step 3: Checkout a specific tag
git checkout v1.0.1

# Step 4 (Optional): Create a new branch from the tag
git checkout -b hotfix/v1.0.1-bugfix

Commands for fetching, checking out, and branching from a remote tag.