Git Describe: name a commit from the nearest tag
git describe names a commit after the nearest tag that is reachable from it. On the tagged commit it prints the tag. After more commits it prints the tag, the number of commits since the tag, and g plus an abbreviated commit id.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-81-A: describe prints the tag on the tagged commit and tag-count-gid after one more commit
Setup for this run, inside a throwaway directory (mktemp -d):
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm one
git tag -a v1.0 -m release
printf 'b\n' >> f.txt; git commit -qam twoCommand:
git describe HEAD~1; git describe | sed 's/-g[0-9a-f]*$/-gHASH/'Recorded output:
v1.0
v1.0-1-gHASHExit status: 0. The output matches the expected text written before the run. This is the status of sed, the last command in the pipe.
TE-81-B: with --tags, git describe also uses a lightweight tag
Setup for this run, inside a throwaway directory (mktemp -d):
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm one
git tag lightCommand:
git describe --tagsRecorded output:
lightExit status: 0. This status and the output match what was written before the run.
TE-81-C: expected non-zero exit: without --tags, git describe finds no annotated tag when only a lightweight tag exists
Setup for this run, inside a throwaway directory (mktemp -d):
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm one
git tag lightCommand:
git describeRecorded output: none, standard output was empty.
Standard error from the same run:
fatal: No annotated tags can describe '29448bae52a576a3f5349c181a183cd48f21c6a0'.
However, there were unannotated tags: try --tags.Exit status: 128. This status matches what was written before the run. The commit id in the first line of standard error changes with the commit contents and differs on each machine and each run.
Reading the recorded output
The first line, v1.0, is the output of git describe HEAD~1. By the setup, HEAD~1 is the first commit, which carries the tag v1.0, so describe printed the tag alone. The second line is git describe on HEAD after the second commit, with the abbreviated id replaced by the text HASH through sed: v1.0-1-gHASH. It has three parts: the tag v1.0, the number 1, and g followed by the masked id. By the setup, one commit was made after the tag, which matches the number 1.
TE-81-B: by the setup, the only tag is a lightweight tag named light. git describe --tags printed light. TE-81-C: the same repository without --tags exited with status 128. Standard error ends with the line However, there were unannotated tags: try --tags.
Limits
TE-81-A uses an annotated tag, and TE-81-B and TE-81-C use a lightweight tag. The reason annotated tags matter here is that git describe ignores lightweight tags by default. The abbreviated id is masked through sed in TE-81-A only, because it changes with the commit contents. TE-81-C records the full commit id in standard error, unmasked.
Source
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-81-A | describe prints the tag on the tagged commit and tag-count-gid after one more commit | 0 | PASS | 8d03c1659617 | | TE-81-B | with --tags, git describe also uses a lightweight tag | 0 | PASS | d51f89f1661a | | TE-81-C | expected non-zero exit: without --tags, git describe finds no annotated tag when only a lightweight tag exists | 128 | PASS | 408f566aeac6 |
Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides