A name for every release
A customer writes that "Tuesday's version" has a defect, and you have thirty commits between Tuesday and today. A commit has a forty-digit hexadecimal identifier, which does not tell you whether it is a release, who it was made for, or which one comes first. A tag solves exactly this: it is a stable name, like v1.0.0, attached to one precise commit. Git has two kinds. The lightweight tag is just the name. The annotated tag is an object of its own, with author, date and message, and the Git documentation names it as the one to use for releases, while the lightweight one is meant for private or temporary labels. The difference is not cosmetic: git describe ignores lightweight tags by default, so a badly labelled release disappears from the version numbers your system generates on its own.
To choose the name you need a rule, and the most widespread is Semantic Versioning: three numbers, MAJOR.MINOR.PATCH, where each number promises something to whoever uses you. In this article you will learn to create both kinds of tag and to see what they contain, to read the distance from the last release with git describe, to list tags in the right order, and to avoid the trap of release candidates. You will then see what happens when you move a tag you have already published. The level is intermediate: it assumes you know commits and branches, and it is for whoever starts releasing software for others, not only working on it.
Every block was run on 2026-11-19 with Git 2.53.0 (git --version), in a test repository created in a temporary folder outside the project. The identifiers in your repository will differ, and where a path was long I replaced it with [...]. The outputs are the real ones, in English as Git prints them with the C locale; the commit messages and tag messages in the examples are in Italian because I ran the same script in both languages.
Lightweight and annotated tags: what they contain
The first block creates a repository with three commits and two tags. The lightweight tag v0.1.0 is a reference that points straight at the commit: if you ask Git for the object type, it answers commit. The annotated tag v0.2.0 is instead an object of type tag, which in turn points to the commit and carries a name, an author, a date and a message. You see it with git cat-file -p. The last line of the block shows the practical consequence: git rev-parse v0.2.0 returns the identifier of the tag object, while adding ^{commit} takes you to the commit underneath. For the lightweight tag the two coincide, because there is nothing in between.
git tag v0.1.0 HEAD~2
git tag -a v0.2.0 -m "Seconda anteprima" HEAD~1
git cat-file -t v0.1.0
# commit
git cat-file -t v0.2.0
# tag
git cat-file -p v0.2.0
# object d22415565f9feb504a5f583ece0dddcd83ed2f93
# type commit
# tag v0.2.0
# tagger Demo <demo@example.invalid> 1795098645 +0100
#
# Seconda anteprima
git rev-parse v0.1.0 v0.2.0 v0.2.0^{commit}
# bab21b0356dc46b6c74113ed660b7d7a5cb0eef0
# 1aa629d680069d648a981120a4c21205036f65ed
# d22415565f9feb504a5f583ece0dddcd83ed2f93
The git tag options you will use most have precise rules. With no options, git tag NAME [COMMIT] creates a lightweight tag and points at HEAD if you give no commit. With -a you create an annotated tag, and if you do not pass -m Git opens the editor for the message. With -m and no -a the tag is annotated anyway: the documentation says -a is implied, and I checked it by creating a throwaway tag and asking for its type, which was tag. With -s the tag is annotated and signed: I did not run this block, because it needs a configured signing key, and the signing backend depends on gpg.format. The reason to prefer the annotated tag is that it records who and when, and release tooling treats it as a real release.
git describe: the distance from the last release
Once your releases are annotated tags, git describe gives a name to any commit: the nearest annotated tag, how many commits sit on top of it, and the abbreviated identifier preceded by g. In the block, right after the v0.2.0 tag, the commit's name is v0.2.0-1-gb2763ea: one commit past the release. After two more commits it becomes v0.2.0-3-g752b8ad. The --abbrev=0 option drops the tail and leaves only the tag, handy in a script. The --dirty option appends -dirty if the working directory has uncommitted changes, which is exactly what you want to know before trusting a version number.
The second part of the block shows the case that surprises. In a repository where only a lightweight tag exists, git describe with no options fails and suggests --tags; with the option it answers v0.1.0. So you can use --tags to include lightweight tags too, but doing so mixes releases with anyone's temporary labels: the annotated tag is precisely the separation between the two.
git describe
# v0.2.0-1-gb2763ea
# (after two more commits)
git describe
# v0.2.0-3-g752b8ad
git describe --abbrev=0
# v0.2.0
# (with an uncommitted change)
git describe --dirty
# v0.2.0-3-g752b8ad-dirty
# In a repository with only a lightweight tag (v0.1.0):
git describe
# fatal: No annotated tags can describe 'fe87150fde29d7c402bc95cb0729431604967a30'.
# However, there were unannotated tags: try --tags.
git describe --tags
# v0.1.0
git describe --always
# fe87150
Listing tags in the right order
With git tag -l (or just git tag) you get the list, and with a quoted pattern you filter: "v0.*" shows only the zero versions. The behaviour that misleads is the order. By default the list is lexicographic, that is by characters, so v0.10.0 appears before v0.2.0 because the character 1 comes before 2. To get version order you need --sort=version:refname, with a leading dash (-version:refname) for descending order. The default sort can be changed with the tag.sort variable, which the documentation names as the source of the default. With -n1 you also see the first line of each annotated tag's message; for a lightweight tag you see the commit subject instead, as in the v0.1.0 row.
git tag -l
# v0.1.0
# v0.10.0
# v0.2.0
# v0.9.0
# v1.0.0
# v1.0.0-rc.1
git tag --sort=-version:refname
# v1.0.0-rc.1
# v1.0.0
# v0.10.0
# v0.9.0
# v0.2.0
# v0.1.0
git tag -l -n1 "v0.*"
# v0.1.0 primo
# v0.10.0 rilascio v0.10.0
# v0.2.0 Seconda anteprima
# v0.9.0 rilascio v0.9.0
git tag --points-at HEAD
# v0.10.0
# v0.9.0
# v1.0.0
# v1.0.0-rc.1
Look at the descending order: v1.0.0-rc.1 appears above v1.0.0, so Git treats it as the newer version. That is wrong under the SemVer precedence rules, which say a pre-release version has lower precedence than the normal version: 1.0.0-alpha < 1.0.0. The remedy is the versionsort.suffix variable, which the git tag documentation cites as a modifier of the version:refname order. The next block tries it: with -c versionsort.suffix=-rc the -rc suffix is sorted before the release, and v1.0.0 comes back on top. If in your flow a command shows "the latest version" with this ordering, without the variable you will end up shipping the candidate last.
git -c versionsort.suffix=-rc tag --sort=-version:refname
# v1.0.0
# v1.0.0-rc.1
# v0.10.0
# v0.9.0
# v0.2.0
# v0.1.0
Which number to use: Semantic Versioning
The SemVer 2.0.0 specification gives a meaning to each part of the X.Y.Z number, with respect to a public API the project must declare. The three rules, as stated on the specification's site, are these:
- MAJOR: increment it when you make incompatible API changes.
- MINOR: increment it when you add functionality in a backward compatible manner.
- PATCH: increment it for backward compatible bug fixes.
It is not a ranking of importance: each number answers a different question from whoever upgrades, namely "will it break something?", "does it give me something new?", "does it fix a defect?". Other rules that matter when working with tags: once a version is released its contents must never be modified, and any change comes out as a new version; version 0.y.z is for initial development, where anything may change at any time; 1.0.0 defines the public API. A pre-release is written with a hyphen and dot-separated identifiers (1.0.0-rc.1), and build metadata with a plus (1.0.0+build.5) does not count in precedence.
One detail concerns Git directly. The specification says v1.2.3 is not a semantic version: the version is 1.2.3, and the v prefix is a common way, with version control, of indicating that the name is a version number. Its own example is git tag v1.2.3 -m "Release version 1.2.3". So the tag name is v1.2.3 and the version is 1.2.3. Choose a prefix and keep it: patterns like "v*" in git tag -l and git describe --match only work if names are consistent.
There is a trade-off to state here. SemVer works well for a library, whose audience consumes an API you can describe; for an application nobody imports, the distinction between MINOR and MAJOR loses meaning, and you risk keeping a number nobody reads. In that case you are not obliged to use SemVer: you can number by date, or with a counter. What stays valid is the annotated tag, because it serves to find the release again, however you name it.
Moving and deleting a tag locally
You tagged the wrong commit. If you have published nothing, the git tag documentation is clear: re-tag it, and -f replaces the old one. Without -f Git refuses: the tag must not exist yet. In the block the first attempt with -a fails with "tag 'v0.9.0' already exists"; the second with -f succeeds and says which object it replaces ("was 0edc771"); then rev-parse confirms the tag now points at HEAD. git tag -d deletes the tag, and the list filtered by v0.9* comes back empty. Note that the value in brackets, in both messages, is the identifier of the tag object that was there before: if you need to go back, that is what you need to note down.
git tag -a v0.9.0 -m "sposto" HEAD
# fatal: tag 'v0.9.0' already exists
git tag -f -a v0.9.0 -m "rifatto" HEAD
# Updated tag 'v0.9.0' (was 0edc771)
git rev-parse --short "v0.9.0^{commit}"
# 3fb9a67
git rev-parse --short HEAD
# 3fb9a67
git tag -d v0.9.0
# Deleted tag 'v0.9.0' (was aac7be8)
git tag -l "v0.9*"
#
After publication the tag does not move
The same documentation distinguishes two cases. If you have already published the tag, the "sane" thing is to admit the mistake and use a different name: whoever has already seen X risks ending up with two people holding "version X" that are really two different versions; you call the new one X.1 and that is that. The "insane" thing is to reuse the name with git tag -f, and in that case Git does not change tags behind users' backs, because people must be able to trust tag names. Whoever already has the old tag must delete it and fetch it again.
The last block reproduces it with a local clone, with no server and no git push: the clone has v1.0.1 on one commit, then in the original repository I move it to another. A plain git fetch origin prints nothing and the tag in the clone does not move. With git fetch --tags origin Git answers with a rejection, "would clobber existing tag", and the tag stays on the old commit. Only after deleting the local tag does the fetch pick it up. It is the concrete form of the risk: whoever does not know keeps a release that no longer matches the one declared by the origin repository.
# in the clone
git rev-parse --short "v1.0.1^{commit}"
# 3fb9a67
# in the original repository: I move the tag
git tag -f -a v1.0.1 -m "rifatto dopo la pubblicazione" HEAD~1
# Updated tag 'v1.0.1' (was 2e6672d)
# in the clone
git fetch origin
# (no output: the tag does not change)
git fetch --tags origin
# From [...]/app
# ! [rejected] v1.0.1 -> v1.0.1 (would clobber existing tag)
git rev-parse --short "v1.0.1^{commit}"
# 3fb9a67
git tag -d v1.0.1
# Deleted tag 'v1.0.1' (was 2e6672d)
git fetch --tags origin
# * [new tag] v1.0.1 -> v1.0.1
git rev-parse --short "v1.0.1^{commit}"
# 752b8ad
Where do tags stand with respect to a remote server? The Pro Git chapter on tags says that by default git push does not transfer tags: you must push them explicitly, with git push origin NAME, or all together with git push origin --tags. I did not run this, because the tests live in a temporary folder with no server, so I report the documentation without a proof of my own. The earlier rule holds: publish a tag only when you are sure of the commit.
Tag and release: from the name to the files
A tag becomes a release when someone uses it to build something. Two commands are enough to see the step. The first is git log v1.0.0..v1.0.1: it lists the commits reachable from the second tag and not from the first, that is what changed between the two releases, and it is the basis of a change list. The second is git archive, which exports the files of a tag without the .git folder; with --prefix you set the archive's root folder, and its name is best derived from the version. The block shows the result: an archive with a single app-1.0.1/ folder.
git tag -a v1.0.1 -m "Correzione"
git log --oneline v1.0.0..v1.0.1
# 3fb9a67 sesto
git archive --format=tar.gz --prefix=app-1.0.1/ -o ../app-1.0.1.tar.gz v1.0.1
tar -tzf ../app-1.0.1.tar.gz
# app-1.0.1/
# app-1.0.1/note.txt
A minimal procedure, in order, for tagging a release:
- bring the work onto the branch you release from and check it is clean with
git status; - choose the number by the SemVer rules relative to the last tag, which you find with
git describe --tags --abbrev=0; - create the annotated tag with
git tag -aand a message saying what the release contains; - check with
git show NAMEandgit describethat it points at the intended commit; - only then publish it.
Order matters here, because a tag can be corrected at zero cost only before the last step: after it, as you have seen, the remedy means changing the name and warning whoever already has it. If you use a flow with release branches, the choice of which commit to label is explained in Git Flow, where the release is born on a dedicated branch. To find the commit where a defect appeared starting from two tags, the method is bisection, described in stash, reflog and bisect.
What you can do now
You can tell a lightweight tag from an annotated one by looking at which object it is, and you know why a release needs the annotated one: it carries author, date and message, and it is the only one git describe considers by default. You can sort tags by version without being fooled by alphabetical order or by release candidates, and choose a number under SemVer, even where the rule is of no use to you. You can move and delete a tag locally, and you know why once published you do not move it: whoever already has it does not receive the change, and the fetch command refuses it. What remains outside is what I could not try here, namely signing with -s and publishing with git push.
Frequently asked questions
What is the difference between a lightweight and an annotated tag?
A lightweight tag is just a name pointing at a commit. An annotated one is an object with author, date and message, and optionally a signature. The Git documentation names it for releases, and git describe ignores lightweight ones by default.
Can I move a tag that is already published?
Technically yes, with git tag -f, but whoever already has the old tag does not receive the change: in the test the fetch rejected it. The documentation advises using a different name, for example X.1.
Is the v prefix part of the semantic version?
No. The SemVer specification says v1.2.3 is not a semantic version: it is the tag name, and the version is 1.2.3. The prefix is a common convention with version control.
Sources
- git-tag - official Git documentation - lightweight and annotated tags,
-aimplied by-m,-f,--sort=version:refname,versionsort.suffixand the "On Re-tagging" section; web page and local manual (git help -m tag) read again on 2026-11-20 - Semantic Versioning 2.0.0 - meaning of MAJOR, MINOR and PATCH, pre-release precedence, version 0.y.z, the note on the
vprefix; read on the web on 2026-11-19 - Pro Git, chapter 2.6 Git Basics - Tagging - the fact that
git pushdoes not transfer tags by default,git push origin NAMEand--tags; read on the web on 2026-11-19, not tried - Examples run with Git 2.53.0 (
git --version) in local test repositories, on 2026-11-19.
Commenti
Caricamento commenti...
Sign in to comment