Sub topic 7
Checking out a tag and using git describe
This chapter assumes you can commit, branch and push, and that you know the difference between lightweight and annotated tags. You will look at the code as it was at a tag, understand why Git calls that a detached HEAD, and use git describe to name any commit relative to your tags.
Checking out a tag
A tag points at one commit and never moves on its own. When you check out a tag, Git puts your working directory in the state of that commit. Because a tag is not a branch, HEAD does not follow a branch name. HEAD points straight at the commit. Git calls this a detached HEAD.
# Look at the code as it was at a tag (detached HEAD)
git switch --detach v1.0.0
# Return to your branch
git switch mainThe --detach flag tells git switch that you want this state on purpose. Without it, git switch v1.0.0 refuses and explains the detached state. The older git checkout v1.0.0 works too, and prints a long warning about detached HEAD.
Committing while detached
Reading code, building it, or running tests at a tag is safe. Trouble starts if you commit while detached. New commits belong to no branch. When you switch away, nothing points at them, and Git will eventually delete them as unreachable.
If you want to keep work started at a tag, create a branch from it:
git switch -c hotfix-1.0 v1.0.0The branch hotfix-1.0 starts at the tagged commit, and your commits now have a name.
Describing a commit with git describe
git describe answers the question "where is this commit relative to my tags?" It finds the nearest tag reachable from HEAD and builds a readable name.
# Describe HEAD relative to the nearest annotated tag
git describeThe output has one of two forms:
v1.1.0when HEAD is exactly at the tag.v1.1.0-4-g2f9c1abwhen HEAD is 4 commits after the tag. The part after-gis the abbreviated hash of HEAD. Thegstands for git.
By default, git describe only considers annotated tags. Lightweight tags are ignored, which is one more reason to prefer annotated tags for releases. Add --tags to include lightweight ones:
git describe --tagsIf no tag is reachable, Git fails with fatal: No names found, cannot describe anything. Add --always to fall back to the abbreviated hash instead.
Common uses
| Command | Result |
|---|---|
| `git describe` | Name based on the nearest annotated tag |
| `git describe --tags` | Also considers lightweight tags |
| `git describe --always` | Falls back to the commit hash if no tag exists |
| `git describe --abbrev=0` | Prints only the tag name, with no commit count or hash |
Build scripts often use git describe --tags --always to stamp a version string into a binary, so each build identifies its exact source commit.
Common errors
- Tag not found after a clone or on another machine. Tags are not pushed by default. Fetch them with
git fetch --tags. git describeignores a tag you just made. The tag is probably lightweight. Use--tags, or recreate it as annotated.- Lost commits after leaving a detached HEAD. Create a branch before switching away, or recover with
git reflog.
Exercise
You run git describe on your current commit and get v2.0.0-3-g8b1d7e4. What does it tell you about the commit, and what would you run to get only v2.0.0?
Show answer
HEAD is 3 commits after the annotated tag v2.0.0, and its abbreviated hash is 8b1d7e4. Running git describe --abbrev=0 prints only v2.0.0.