Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Create a Tag

git tag -a v1.2.0 -m "Release 1.2.0"

This creates an annotated tag on the commit you are on. Without -m, Git opens your editor for the message. For a quick private marker, a lightweight tag needs only a name:

git tag before-upgrade

Lightweight and Annotated Tags

TypeWhat it stores
LightweightOnly a name for a commit
AnnotatedName, tagger, date, message

A lightweight tag is like a branch that never moves. An annotated tag is a separate object in the repository with its own tagger, date, and message, and it can be signed. Use annotated tags for releases and anything you publish; keep lightweight tags for local bookmarks.

Tag an Older Commit

Find the commit's hash and name it at the end:

git log --oneline -10
git tag -a v1.1.1 a1b2c3d -m "Release 1.1.1"

List and Inspect Tags

CommandWhat it shows
git tagAll tags
git tag -l "v1.2.*"Tags matching a pattern
git tag -nTags with their message
git show v1.2.0Tag details and the commit
git describe --tagsNearest tag to HEAD

By default, git tag sorts names as text, so v1.10.0 comes before v1.9.0. To sort by version number, newest first:

git tag --sort=-version:refname

Push Tags

git push origin v1.2.0

This sends one tag. git push origin --tags sends every local tag, including private markers you may not want to publish. A tidier habit is to push commits and their annotated tags together:

git push --follow-tags

It sends annotated tags that point at the commits being pushed, and skips lightweight ones. To make it the default, run git config --global push.followTags true. Pushing in general is covered in git push: upload commits to a remote.

Delete a Tag

git tag -d v1.2.0
git push origin --delete v1.2.0

The first line deletes the tag in your clone, the second on the remote. Clones that already fetched the tag keep it until their owners delete it too.

Check Out a Tag

git switch --detach v1.2.0

This puts your files at the tagged release without a branch, which is a detached HEAD; see Git detached HEAD. To fix something in an old release, start a branch from the tag instead:

git switch -c hotfix/1.2.1 v1.2.0

Do Not Move a Published Tag

git tag -f can move a tag to another commit, but anyone who fetched the old one keeps it: Git does not change a tag that already exists in a clone behind its owner's back. Two people then build different code under the same version name. If a release was wrong, tag the fix as a new version, such as v1.2.1.

Signed Tags and Version Names

git tag -s creates an annotated tag signed with your GPG key, or your SSH key if Git is set up for SSH signing, and git tag -v checks the signature.

For names, most projects follow semantic versioning: MAJOR.MINOR.PATCH with a v in front. Raise the major number for incompatible changes, the minor number for new features, and the patch number for fixes. Pre-releases get a suffix, such as v2.0.0-rc.1. A tag is a ref like a branch, so it has the same naming limits as a branch, explained in how to create a branch in Git.

Was this answer helpful?
Loading...