More Git answers

by Danny
Published 2 min read

Create an annotated tag for a release, push it to the remote, and fix or delete a tag you got wrong, with the difference between lightweight and annotated tags.

The short answer

#

Create an annotated tag with git tag -a, then push it by name. Tags are not sent by a plain git push, so you have to push them explicitly.


_10
git tag -a v1.4.0 -m "Release 1.4.0"
_10
git push origin v1.4.0

Why it happens

#

A tag is a name that points at one commit and, unlike a branch, does not move when you make new commits. That makes it the natural way to mark a release.

Git has two kinds:

  • Lightweight tags, made with git tag v1.4.0, are just a name for a commit, like a branch that never moves.
  • Annotated tags, made with -a, are full objects in the repository with their own message, tagger name, date and optional signature.

For releases, use annotated tags. They record who made the release and when, git describe uses them by default, and git push --follow-tags only pushes annotated tags.

Tags are kept out of git push by default so that local bookmarks do not end up on the shared remote by accident.

How to fix it

#

Tag the current commit

Make sure you are on the commit you are releasing, usually the tip of main, then tag it:


_10
git switch main
_10
git pull
_10
git tag -a v1.4.0 -m "Release 1.4.0"
_10
git show v1.4.0 # check the tag message and the commit it points at

Leave off -m to write a longer message, such as release notes, in your editor.

Tag an earlier commit

Pass the commit hash after the tag name:


_10
git log --oneline -10
_10
git tag -a v1.3.1 8d2f6b0 -m "Release 1.3.1"

Push tags to the remote

Push a single tag by name:


_10
git push origin v1.4.0

Or push your commits and any annotated tags that point at them in one step:


_10
git push --follow-tags

To make that the default for every push:


_10
git config --global push.followTags true

git push --tags also works but sends every local tag, including old experiments, so it is better to push by name.

List and inspect tags


_10
git tag # all tags
_10
git tag -l "v1.*" # tags matching a pattern
_10
git describe --tags # nearest tag to the current commit

Fix or delete a tag

If you tagged the wrong commit and have not pushed yet, delete it locally and create it again:


_10
git tag -d v1.4.0
_10
git tag -a v1.4.0 -m "Release 1.4.0"

If it is already on the remote, delete it there too:


_10
git push origin --delete v1.4.0

Once other people or a deploy pipeline have used a tag, it is usually better to release a new version, such as v1.4.1, than to move the old one. Clones that already fetched the tag keep the old commit and will not update it automatically.

Sign release tags

If your team verifies releases, create a signed tag with -s instead of -a. It needs a GPG or SSH signing key configured in Git.


_10
git tag -s v1.4.0 -m "Release 1.4.0"
_10
git tag -v v1.4.0 # verify the signature

Common pitfalls

#
  • Expecting git push to send tags. It does not, unless you use --follow-tags, --tags or push the tag by name.
  • Using lightweight tags for releases, then wondering why --follow-tags skipped them.
  • Giving a tag the same name as a branch. Commands then become ambiguous; a v prefix such as v1.4.0 avoids clashes.
  • Moving a published tag. Other clones keep the old one, so people end up building different code for the same version.
  • Tagging before pulling, so the tag points at an out of date commit.
#

Was this resource helpful?