Sub topic 3
Lightweight and annotated tags
This chapter assumes you can commit, branch and push, and that you know a commit has a hash. It covers the two kinds of tag Git offers and when to use each.
Two kinds of tag
A tag is a name that points at a commit. Git stores it in two different ways.
- A lightweight tag is only a name. It is a reference that holds a commit hash, much like a branch that never moves.
- An annotated tag is a separate object in Git's database. It stores the tagger's name and email, the date, a message, and a pointer to the commit.
Creating each kind
Both commands tag the commit you currently have checked out (HEAD).
# Lightweight tag on the current commit
git tag v1.0.0
# Annotated tag with a message
git tag -a v1.1.0 -m "Release 1.1.0"The -a option makes the tag annotated, and -m supplies the message. If you pass -a without -m, Git opens your editor so you can write the message.
Seeing the difference
List tags that match a pattern, then show one.
# List tags matching a pattern
git tag -l "v1.*"
# Show the tag metadata and the commit it points to
git show v1.1.0For the annotated tag, git show prints the tagger, the date and the message, followed by the commit. For a lightweight tag it prints only the commit, because no tag object exists.
Which one to use
| Question | Lightweight | Annotated |
|---|---|---|
| Stores tagger, date, message | No | Yes |
| Can be signed | No | Yes |
| Sent by `git push --follow-tags` | No | Yes |
| Used by `git describe` by default | No | Yes |
| Good for | Private bookmarks | Published versions |
Use annotated tags for anything you share, such as a release. They record who tagged the version and when, and several Git features only consider them. Use lightweight tags for temporary markers on your own machine.
Exercise
You run git tag v2.0.0 and later git show v2.0.0. The output has no tagger line and no message. Why, and what command would have recorded them?
Show answer
git tag v2.0.0 creates a lightweight tag, which has no tag object to hold a tagger, date or message, so git show displays only the commit. Creating it with git tag -a v2.0.0 -m "Release 2.0.0" makes an annotated tag that stores that information.
Exercise
Which kind of tag will git push --follow-tags send along with your commits?
Show answer
Only annotated tags that point to commits being pushed. Lightweight tags are skipped.