Browse topics

git tag

Version
2.51
Review
Draft

Summary#

git tag names a commit or other object. An annotated tag, made with -a, is its own object with a tagger and message and suits releases. A lightweight tag is only a ref. --sort=version:refname orders version numbers numerically.

Syntax#

sh
git tag -a v1.2.0 -m "Release 1.2.0"
git tag --list --sort=version:refname 'v*'
git push origin tag v1.2.0
git push --follow-tags
git tag -d v1.2.0

Example#

sh
(
  set -eu
  scratch=$(mktemp -d "${TMPDIR:-/tmp}/devlookup-git.XXXXXX")
  trap 'rm -rf -- "$scratch"' EXIT
  unset GIT_CONFIG_GLOBAL $(git rev-parse --local-env-vars)
  export GIT_CONFIG_NOSYSTEM=1 GIT_TEMPLATE_DIR= LC_ALL=C
  export HOME="$scratch" XDG_CONFIG_HOME="$scratch" GIT_ATTR_NOSYSTEM=1
  export GIT_PAGER=cat GIT_EDITOR=true
  git init -q --bare -b trunk "$scratch/remote.git"
  git init -q -b trunk "$scratch/work"
  cd "$scratch/work"
  git config user.name "Reference Example"
  git config user.email example@example.invalid
  git commit -q --allow-empty -m "Release candidate"
  git remote add origin "$scratch/remote.git"

  git tag -a v1.9.0 -m "Release 1.9.0"
  git tag -a v1.10.0 -m "Release 1.10.0"
  git tag checkpoint
  git tag --list --sort=version:refname --format='%(refname:short) %(objecttype)' 'v*'
  git cat-file -t checkpoint

  git push -q origin trunk
  echo "remote tags after branch push: $(git ls-remote --tags origin | wc -l | tr -d ' ')"
  git push -q origin tag v1.10.0
  echo "remote:"
  git --git-dir="$scratch/remote.git" tag --list
  git tag -d checkpoint > /dev/null
  echo "local, default order:"
  git tag --list
)

Expected output#

Text
v1.9.0 tag
v1.10.0 tag
commit
remote tags after branch push: 0
remote:
v1.10.0
local, default order:
v1.10.0
v1.9.0

Common mistakes#

Pushing a branch does not by itself send your tags unless configuration asks for it. Push the tag with git push origin tag NAME, or use --follow-tags, which includes only missing annotated tags reachable from what you push. git tag -d deletes the local tag and leaves the remote one.

Version applicability#

Git 1.8.3 added --follow-tags and Git 2.0 added version sorting for tags.

The complete example requires Git 2.28 or later for git init -b.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0