How do you create and use tags in Git?
Tags mark specific commits as releases or milestones. Annotated tags include a message and are the standard for versioning.
GitVersion Control
Overview
Tags are pointers to specific commits, typically used to mark release versions (v1.0.0, v2.3.1). Unlike branches, tags do not move as new commits are added. There are two kinds: lightweight (just a pointer) and annotated (full objects with a message, date, and tagger info).
Key Points
- •Annotated tags (`git tag -a`) are the standard for releases - they carry metadata.
- •Lightweight tags are just an alias for a commit hash, with no extra metadata.
- •Tags are NOT pushed automatically with `git push` - use `git push origin --tags`.
- •Delete a local tag: `git tag -d v1.0.0`. Delete a remote tag: `git push origin --delete v1.0.0`.
- •List tags: `git tag` or `git tag -l "v1.*"` for filtered patterns.
Examples
Create an annotated release tag
bashgit tag -a v1.2.0 -m "Release v1.2.0: add dark mode and fix login bug"Tag a past commit
bashgit log --oneline
# a1b2c3d fix: resolve auth timeout
git tag -a v1.1.1 a1b2c3d -m "Patch release: fix auth timeout"Push tags to remote
bash# Push a single tag
git push origin v1.2.0
# Push all local tags
git push origin --tagsList and inspect tags
bashgit tag # list all tags
git tag -l "v1.*" # filter tags
git show v1.2.0 # inspect tag details + commitCheck out code at a specific tag
bashgit checkout v1.2.0
# This puts you in "detached HEAD" state
# To work on it, create a branch:
git switch -c hotfix/v1.2.1Pro Tip
Follow Semantic Versioning (semver): `MAJOR.MINOR.PATCH`. Increment MAJOR for breaking changes, MINOR for new features, PATCH for bug fixes. Tools like `standard-version` or GitHub Releases can automate tag creation based on commit messages.