Master Git in 30 Days

Day 15: Tags - Marking Important Events in Version Control

When using Git for version control, a large number of versions will be generated. Over time, among these many versions, there will definitely be some important versions worth recording. This is what “tags” can help us do.

Understanding Tags - Basic Concepts

Basically, the purpose of a tag is to mark a specific “version” or “commit object” with an “easy-to-remember name” to help us remember a particular version.

A tag is a reference that points to a specific commit in your Git history. Unlike branches, tags don’t move when you make new commits - they always point to the same commit. This makes tags ideal for marking release points or other significant milestones.

Git supports two types of tags:

  1. Lightweight tags: Simply a name (alias) for a specific commit - it’s a type of “reference name”
  2. Annotated tags: Stored as full Git objects with additional metadata

As mentioned in Day 11 (Understanding Git Object References), “general references” include “tag names,” and the “tag name” here refers to “lightweight tags.” So “lightweight tags” can be said to be just an “alias” for a certain commit version, which is a type of “relative name.”

“Annotated tags” are a type of Git object. As mentioned in Day 06 (Parsing Git Data Structure - Object Structure), Git objects include 4 object types: Blob, Tree, Commit, and Tag objects. The “annotated tag” mentioned here is a Tag object. Tag objects are stored in Git’s object storage (stored in the .git\objects\ directory) and are associated with another commit object. When creating “annotated tags,” you can include “version messages” just like when creating commit objects. In Git’s built-in tag mechanism, you can even use GnuPG keys to sign Tag objects to ensure message “non-repudiation.”

Although we have these two tag types, and both look like “aliases” for commit objects, the usage of these two tags only has slight differences. In most use cases, we use “annotated tags” to create “tag objects” and provide “version messages,” because this type of “tag” is a “permanent object” in the Git repository. (Git objects stored in the object storage are immutable; only the index changes.)

Creating Lightweight Tags

The simplest way to create a tag:

git tag v1.0

This creates a lightweight tag named v1.0 pointing to the current commit (HEAD).

To tag a specific commit:

git tag v0.9 <commit-hash>

Note: All files under .git\refs\ are “reference names.”

Basic usage:

  • List all tags: git tag
  • Create lightweight tag: git tag [tagname]
  • Delete lightweight tag: git tag [tagname] -d

If you want to see the content of a “lightweight tag,” you can use git cat-file -p [tagname]. If you want to see the object type of the “lightweight tag,” you can use git cat-file -t [tagname].

Important Note: A lightweight tag is not a Git object. From the above, you can see that the tag name retrieves the content of the commit object, and the object type found by the name is a commit object.

Creating Annotated Tags

Annotated tags are recommended for most use cases because they store extra information:

git tag -a v1.0 -m "Release version 1.0"

The -a flag creates an annotated tag, and -m specifies the tag message.

Differences from lightweight tags:

  1. When executing git tag to list all tags, all tags are mixed together and you can’t see the tag type
  2. Creating “annotated tags” requires the -a parameter
  3. Creating “annotated tags” must include a “version message,” just like executing git commit, both have the -m parameter available

Listing Tags

To see all tags in your repository:

git tag

To see tags matching a pattern:

git tag -l "v1.*"

Viewing Tag Information

For a lightweight tag:

git show v1.0

This shows the commit the tag points to.

For an annotated tag:

git show v1.0

This shows the tag information plus the commit details.

Tagging Old Commits

You can tag commits that are not at HEAD:

git log --oneline  # Find the commit hash
git tag -a v0.5 -m "Beta release" <commit-hash>

Pushing Tags to Remote

By default, git push doesn’t transfer tags. You need to explicitly push them:

# Push a specific tag
git push origin v1.0

# Push all tags
git push origin --tags

Deleting Tags

To delete a local tag:

git tag -d v1.0

To delete a remote tag:

git push origin :refs/tags/v1.0
# or
git push origin --delete v1.0

Checking Out Tags

You can check out the code at a specific tag:

git checkout v1.0

This puts you in a “detached HEAD” state. If you want to make changes, create a new branch:

git checkout -b version-1-fixes v1.0

Today’s Summary

Tags are an essential part of version control, allowing you to mark important points in your project’s history. Annotated tags are particularly useful for releases as they can store additional information like release notes, tagger information, and signatures.

Let me reorganize the Git commands and parameters learned today:

  • git tag
  • git tag -a
  • git tag -l
  • git show
  • git push origin –tags
  • git tag -d
  • git checkout