How it works
The explanation, with examples you can run.
Both kinds side by side
git init -q -b main demo && cd demofor n in 1 2 3; do echo "v$n" > app.txt; git add app.txt; git commit -q -m "Change $n"; donegit tag before-cleanup HEAD~1git tag -a v1.0.0 -m "First stable release"git cat-file -t before-cleanupgit cat-file -t v1.0.0git cat-file -p v1.0.0committagobject 9e15937b1e10978746a883d43a16c594f985c2b2type committag v1.0.0tagger Ada <ada@example.com> 1767607200 +0000 First stable releasebefore-cleanup resolves straight to a commit. v1.0.0 is a tag object of its own that points at the commit, which is the fourth kind of object after blobs, trees and commits. git tag -s (or git config tag.gpgSign true) adds a signature, as described in the signing topic.
Name builds after tags
echo "v4" > app.txt && git commit -q -am "Change 4"git describegit describe --abbrev=0v1.0.0-1-g9bd4269v1.0.0git describe finds the nearest annotated tag and says how far you are from it: one commit past v1.0.0, at commit 9bd4269 (the g stands for git). Build scripts often stamp this into a binary so you can tell exactly what is running. It ignores lightweight tags unless you add --tags.
Publishing tags
A plain git push sends branches, not tags. Push a release tag by name, or let annotated tags travel with the commits they point at:
git push origin v1.0.0git push --follow-tagsgit config --global push.followTags trueHosting services build on tags. A GitHub or GitLab release is a tag plus notes and attached build artefacts, and CI pipelines often run a release job when a tag matching v* is pushed.
Rules of thumb
- Version numbers. Most projects use semantic versioning (
MAJOR.MINOR.PATCH, with avprefix by convention), so the tag tells users whether an upgrade can break them. - Never move a published tag.
git tag -fand a forced push replace the tag on the server, but clones that already fetched it keep the old one, and fetching refuses to replace it (would clobber existing tag). Tag a new version instead. - Tags are not branches. To fix an old release, create a branch from its tag (
git switch -c release-1.0 v1.0.0), commit the fix and tagv1.0.1. - Fetching.
git fetchbrings tags that point into the history it downloads;git fetch --tagsfetches all of them.
Check yourself
3 questions. Get them all right to finish this topic.
0 of 3 right so far. Get all three to finish this topic.