Tags and Releases: Lightweight and Annotated Tags, describe and archive
Mark releases with Git tags: lightweight versus annotated tags, creating, listing, sorting and filtering them, tagging older commits, pushing and deleting tags, why moving a tag is a problem, version names from git describe, and release files from git archive.
kvi5hnuCode Sprout team member6 min readOn this page
A tag is a name for a specific commit that, unlike a branch, doesn't move. Tags mark releases (v1.0.0, v2.3.1), and they're how you, your users and your build system find the exact code that was shipped. This article covers the two kinds of tag, working with them locally and on remotes, and the commands that turn tags into version numbers and release files.
Lightweight and annotated tags
git init -q trail-guide && cd trail-guide
git commit -q --allow-empty -m "Add lake page"
git tag v0.9 # lightweight
git commit -q --allow-empty -m "Add ridge page"
git tag -a v1.0 -m "Version 1.0: lake and ridge" # annotated
git tag -n # tags with their first message line
echo "=== what each tag name points to"
echo "v0.9 -> $(git cat-file -t v0.9)"
echo "v1.0 -> $(git cat-file -t v1.0)"
echo "=== the annotated tag object"
git cat-file -p v1.0
v0.9 Add lake page
v1.0 Version 1.0: lake and ridge
=== what each tag name points to
v0.9 -> commit
v1.0 -> tag
=== the annotated tag object
object b75b0fc6e0fadaf2f91733921d5222c1eee65c8e
type commit
tag v1.0
tagger Sam Lee <sam@example.com> 1772445600 +0000
Version 1.0: lake and ridge
- A lightweight tag (
git tag <name>) is just a name pointing directly at a commit, like a branch that never moves. - An annotated tag (
git tag -a <name> -m "...") is a separate tag object stored in the repository, with the tagger's name, email and date, a message, and optionally a signature (article 39). It points to the commit.
Use annotated tags for releases: they record who released what and when, git describe uses them by default, and git push --follow-tags only pushes annotated tags. Lightweight tags suit private bookmarks.
git tag -a v1.0 without -m opens your editor for the message. -s creates a signed annotated tag.
Tagging an older commit, and inspecting tags
Tags normally go on the current commit, but any commit can be tagged:
git tag -a v0.8 -m "Version 0.8" a1b2c3d # tag an older commit
git show v1.0 # the tag's details, then the commit
git show v1.0 --no-patch # without the diff
git log --oneline --decorate v0.9..v1.0 # what changed between two releases
Listing, sorting and filtering
git init -q trail-guide && cd trail-guide
for v in 1.2 1.9 1.10 2.0; do
git commit -q --allow-empty -m "Release $v"
git tag -a "v$v" -m "Version $v"
done
git switch -q -c hotfix HEAD~1
git commit -q --allow-empty -m "Hotfix"; git tag -a v1.10.1 -m "Version 1.10.1"
git switch -q main
echo "=== default order is alphabetical"
git tag -l "v1*"
echo "=== version order, newest first"
git tag -l "v1*" --sort=-version:refname
echo "=== which tags contain the commit 'Release 1.9'?"
git tag --contains main~2
echo "=== which tags are reachable from main?"
git tag --merged main --sort=version:refname
=== default order is alphabetical
v1.10
v1.10.1
v1.2
v1.9
=== version order, newest first
v1.10.1
v1.10
v1.9
v1.2
=== which tags contain the commit 'Release 1.9'?
v1.10
v1.10.1
v1.9
v2.0
=== which tags are reachable from main?
v1.2
v1.9
v1.10
v2.0
-l "<pattern>"filters by name, with shell wildcards.--sort=version:refnamesorts version numbers properly, sov1.10comes afterv1.9. Settag.sort=version:refnameto make it the default (article 33).-reverses the order.--contains <commit>lists the tags that include a commit: "which releases contain this fix?".--no-containslists the opposite.--merged <branch>lists tags reachable from a branch, here leaving out the hotfix tag, and--points-at <commit>the tags on exactly that commit.--sort=-creatordatelists the most recently created tags first.
Tags on remotes
Tags aren't pushed with branches by default (article 20):
git push origin v1.0 # one tag
git push --follow-tags # branches, plus annotated tags on pushed commits
git push origin --tags # every local tag
git push origin --delete v1.0 # delete a tag on the remote
git tag -d v1.0 # delete it locally
git ls-remote --tags origin # tags on the remote
git fetch downloads tags that point to commits it fetches, and git fetch --tags fetches all of them. Set push.followTags=true to make --follow-tags the default.
Don't move published tags
Tags are meant to be permanent: once people have fetched v1.0, Git won't silently change their copy, even if you move the tag on the server:
git init -q --bare server.git
git clone -q server.git sam 2>/dev/null; cd sam
git commit -q --allow-empty -m "Release 1.0"; git tag -a v1.0 -m "Version 1.0"
git push -q origin main v1.0
cd ..; git clone -q server.git ana
cd sam # Sam moves the tag and force pushes it
git commit -q --allow-empty -m "Last-minute fix"
git tag -f -a v1.0 -m "Version 1.0 (fixed)" > /dev/null
git push -q --force origin v1.0
cd ../ana
git fetch; echo "plain fetch: exit $?, v1.0 is still '$(git log -1 --format=%s v1.0)'"
git fetch --tags; echo "fetch --tags: exit $?"
git fetch -q --tags --force; echo "after --force: v1.0 is '$(git log -1 --format=%s v1.0)'"
plain fetch: exit 0, v1.0 is still 'Release 1.0'
From /workspace/server
! [rejected] v1.0 -> v1.0 (would clobber existing tag)
fetch --tags: exit 1
after --force: v1.0 is 'Last-minute fix'
A plain fetch kept the old v1.0 without a word, --tags refused with would clobber existing tag, and only --force updated it. Anyone who built v1.0 before has different code from anyone after, which defeats the point of a tag. If a release was wrong, release a new version (v1.0.1) instead of moving the tag. Hosting services can also protect tag patterns from being moved or deleted.
Also note that git tag -f <name> without -a replaces an annotated tag with a lightweight one, losing its message.
Version names: git describe
git describe names the current commit relative to the most recent annotated tag:
git init -q trail-guide && cd trail-guide
git commit -q --allow-empty -m "Release 1.0"; git tag -a v1.0 -m "Version 1.0"
git commit -q --allow-empty -m "Add maps"
git commit -q --allow-empty -m "Add photos"
git describe # tag, commits since, g + abbreviated ID
git describe --abbrev=0 # just the tag
echo "draft" > notes.txt; git add notes.txt
git describe --dirty # -dirty when there are uncommitted changes
git switch -q --detach v1.0
git describe # exactly on a tag: just its name
v1.0-2-gc92bc9b
v1.0
v1.0-2-gc92bc9b-dirty
v1.0
v1.0-2-g<id> means "two commits after v1.0, at commit <id>" (the g stands for Git). It's unique and sortable enough for build numbers, and many build systems embed it in the program's version string. Options: --tags also considers lightweight tags, --match "v*" and --exclude limit which tags count, --always falls back to the commit ID when there's no tag, and --long always shows the full form.
Release files: git archive
git archive creates a tar or zip file of a commit's files, without the .git folder, which is the usual way to make source release downloads:
git init -q trail-guide && cd trail-guide
mkdir -p src tests; echo "app" > src/app.js; echo "test" > tests/app.test.js
echo "ci" > .ci.yml
printf 'tests/ export-ignore\n.ci.yml export-ignore\n.gitattributes export-ignore\n' > .gitattributes
git add .; git commit -q -m "Release 1.0"; git tag -a v1.0 -m "Version 1.0"
git archive --format=tar --prefix=trail-guide-1.0/ v1.0 | tar -t
git archive --format=zip --prefix=trail-guide-1.0/ -o trail-guide-1.0.zip v1.0
ls -1 *.zip
trail-guide-1.0/
trail-guide-1.0/src/
trail-guide-1.0/src/app.js
trail-guide-1.0.zip
--prefix=<folder>/puts every file in a top-level folder, as users expect when unpacking. Don't forget the trailing/.--formatistar,tar.gz,tgzorzip, and-o <file>picks the format from the extension.- Paths marked
export-ignorein.gitattributesare left out (article 35), here tests and CI files. - Give a path to archive only part of the tree:
git archive v1.0 docs/.
Releases on hosting services
GitHub, GitLab and Bitbucket build releases on top of tags: a page per tag with release notes, downloadable source archives made with git archive, and uploaded files such as installers. They can generate release notes from the pull requests merged since the previous tag. Tools such as semantic-release and release-please go further, choosing the next version number from Conventional Commit messages (article 52), tagging, and publishing.
Most projects number releases with semantic versioning, MAJOR.MINOR.PATCH: increase MAJOR for incompatible changes, MINOR for new features, and PATCH for fixes. The v prefix is a common convention.
What's current
- Annotated tags with semantic version names (
v2.3.1) are the norm, and hosting services build release pages on them. git push --follow-tagsandpush.followTagspush release tags together with their commits.- Hosting services offer tag protection rules that stop release tags from being moved or deleted.
Common questions
What's the difference between a tag and a branch? Both are names for commits. A branch moves forward with each new commit on it. A tag stays on the commit it was created on.
Should I use lightweight or annotated tags? Annotated for anything shared, especially releases: they store who tagged, when, and a message, and can be signed. Lightweight tags are fine for personal markers.
I pushed a tag to the wrong commit. How do I fix it?
If nobody has fetched it yet, delete and recreate it: git tag -d v1.0, git push origin --delete v1.0, git tag -a v1.0 -m "..." <right-commit>, and push again. If others may have it, create a new version instead, because their clones won't update the tag automatically.
How do I check out the code for a tag?
git switch --detach v1.0 to look at it, or git switch -c fix-1.0 v1.0 to create a branch there, for example to prepare a patch release.
Summary
- Tags name a commit permanently. Lightweight tags are plain names, and annotated tags (
-a) store tagger, date, message and an optional signature. Use annotated tags for releases. git tag -l,--sort=version:refname,--contains,--mergedand--points-atfind tags.- Push tags explicitly (
git push origin v1.0,--follow-tags), and delete them withgit push origin --delete. - Don't move published tags: clones keep the old one. Release a new version instead.
git describeproduces version strings likev1.0-2-g<id>, andgit archive --prefixbuilds release archives, honouringexport-ignore.
Practice questions
You're releasing version 2.4.0 from the current commit on main. Which commands create and publish the tag?
Answer
git tag -a v2.4.0 -m "Version 2.4.0"
git push origin v2.4.0 # or: git push --follow-tags
An annotated tag records who released it and when. If you use push.followTags=true, a normal git push sends it along with the commits.
A bug fix is commit a1b2c3d. How do you find which released versions already contain it?
Answer
git tag --contains a1b2c3d, optionally with --sort=version:refname. If the fix was cherry-picked to release branches (article 16), those copies have different IDs, so also search by message: git log --all --oneline --grep="<fix subject>", and check tags containing each copy.
What does git describe print for a build three commits after the tag v3.1.0, with uncommitted changes, using --dirty?
Answer
Something like v3.1.0-3-g9c8d7b6-dirty: the nearest annotated tag, the number of commits since it, g followed by the abbreviated commit ID, and -dirty because the working tree has changes.
Previous: Git Tooling: Shell Prompt and Completion, GUI Clients, and External Diff and Merge Tools

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.