Git by Example: Tag Creation
Learn how to mark specific points in your project history using tags. This example covers both lightweight tags and annotated tags for release management.
Code
# 1. Create a lightweight tag (bookmark)
git tag v1.0
# 2. Create an annotated tag (recommended for releases)
git tag -a v1.1 -m "Release version 1.1"
# 3. Tag a specific commit in the past
git tag -a v0.9 9fceb02 -m "Beta release"
# 4. View tag details
git show v1.1
# Output:
# Tagger: John Doe <[email protected]>
# Date: Mon Oct 2 10:00:00 2023 -0700
#
# Release version 1.1
#
# commit 1a2b3c...Explanation
Git tags are used to mark specific points in a repository's history as being important. Typically, people use this functionality to mark release points (v1.0, v2.0, etc.). Unlike branches, which are expected to move and change, tags are meant to be permanent pointers to a specific commit.
There are two types of tags: lightweight and annotated. A lightweight tag is very much like a branch that doesn't change—it's just a pointer to a specific commit. An annotated tag, however, is stored as a full object in the Git database. It contains the tagger name, email, and date; has a tagging message; and can be signed and verified with GNU Privacy Guard (GPG).
It is generally recommended to use annotated tags for all your public releases so that you have all this metadata stored. Lightweight tags are useful for temporary object labels or for private use where you just need a quick bookmark.
- Tags mark specific, permanent points in history
- Lightweight tags are simple pointers
- Annotated tags store full metadata and messages
- Use annotated tags (
-a) for releases
Code Breakdown
-a flag specifies annotated, and -m allows you to add a message, similar to a commit message.
