KKAIROS
Practical guideIllustrative4 min read

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.

Published:

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.sha

Paths 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=0

Check 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=yes

TE-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.0

Command:

git tag -l 'v1*'

Recorded output:

v1.0
v1.1

Exit 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 v1

Command:

git tag v1

Recorded output: none, standard output was empty.

Standard error from the same run:

fatal: tag 'v1' already exists

Exit 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.

Ad

Limits

Tags can be moved with -f and deleted with -d; they do not follow new commits.

Source

Evidence

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