git tag: name a commit, lightweight and annotated
A tag is a name for an object, usually a commit; lightweight tags point at the commit, annotated tags are their own objects.
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-15-A: lightweight tag points at a commit, an annotated tag is its own object, and a tag can be moved with -f
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 'one\n' > f.txt; git add f.txt; git commit -qm one; git rev-parse HEAD > ../c1.sha
printf 'two\n' >> f.txt; git add f.txt; git commit -qm two; git rev-parse HEAD > ../c2.shaPaths written as ../name sit one level above the repository folder, inside the same throwaway tree, so they do not appear in git status.
Command:
git tag light $(cat ../c1.sha); git tag -a ann -m msg $(cat ../c1.sha); git tag -f light $(cat ../c2.sha) >/dev/null; echo tagged=$?Recorded output:
tagged=0Check command and its recorded output:
echo light_type=$(git cat-file -t light); echo ann_type=$(git cat-file -t ann); echo moved=$([ "$(git rev-parse light)" = "$(cat ../c2.sha)" ] && echo yes)light_type=commit
ann_type=tag
moved=yesTE-15-B: git tag -l with a pattern lists only the matching tag names
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 'one\n' > f.txt; git add f.txt; git commit -qm one
git tag v1.0; git tag v1.1; git tag v2.0Command:
git tag -l 'v1*'Recorded output:
v1.0
v1.1Exit status: 0. This status and the output match what was written before the run.
TE-15-C: expected non-zero exit: creating a tag whose name already exists is refused
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 'one\n' > f.txt; git add f.txt; git commit -qm one
git tag v1Command:
git tag v1Recorded output: none, standard output was empty.
Standard error from the same run:
fatal: tag 'v1' already existsExit status: 128. This status and the output match what was written before the run.
Reading the recorded output
The line tagged=0 is the exit status of the last tag command in the run, the forced move of light. The check lines then show that light is a commit object, so a lightweight tag here is only a name for a commit, while ann is a tag object of its own. The line moved=yes shows that after git tag -f pointed light at the second commit, light resolved to the id of that second commit.
TE-15-B: by the setup, the tags are v1.0, v1.1 and v2.0. git tag -l 'v1*' printed v1.0 and v1.1. TE-15-C: by the setup, the tag v1 already existed. git tag v1 exited with status 128 and printed fatal: tag 'v1' already exists.
Limits
Tags can be moved with -f and deleted with -d; they do not follow new commits.
Source
Evidence
TE-15-A: lightweight tag points at a commit, an annotated tag is its own object, and a tag can be moved with -f
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-15-A | lightweight tag points at a commit, an annotated tag is its own object, and a tag can be moved with -f | 0 | PASS | 79aa78d64b65 | | TE-15-B | git tag -l with a pattern lists only the matching tag names | 0 | PASS | 882079ab431a | | TE-15-C | expected non-zero exit: creating a tag whose name already exists is refused | 128 | PASS | ff76d3bb3d06 |
Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides