Sub topic 6
Pushing tags to a remote
This chapter assumes you can commit, branch and push, and that you have created lightweight and annotated tags. It covers how tags travel to a remote such as GitHub, and the mistakes that come up when they don't.
Tags are not pushed by default
A plain git push sends commits on your branch. It does not send tags. If you tag a commit and push, your teammates and GitHub will not see the tag. Tags are separate references, and you have to share them on purpose.
Three ways to push tags
# Push one tag
git push origin v1.1.0
# Push all tags
git push origin --tags
# Push commits plus the annotated tags that point into them
git push --follow-tagsThe table compares them.
| Command | What it sends | Typical use |
|---|---|---|
| `git push origin <tag>` | One named tag | Publishing a single release |
| `git push origin --tags` | Every local tag, lightweight and annotated | Initial upload of many tags |
| `git push --follow-tags` | Annotated tags reachable from the commits being pushed | Everyday pushes |
Pushing a tag by name is the safest choice because you know exactly what leaves your machine. --tags sends everything, including temporary lightweight tags you made for testing. --follow-tags skips lightweight tags, which is one more reason to use annotated tags for releases.
Checking what the remote has
List the tags on the remote without fetching anything:
git ls-remote --tags originAnnotated tags appear twice. The entry ending in ^{} shows the commit the tag points to. The other entry is the tag object itself.
To download tags that others pushed, run git fetch --tags. A normal git fetch also downloads tags that point into history it is already fetching.
Common errors
- The tag is missing on GitHub. You ran
git pushwithout naming the tag. Push it explicitly. rejected ... already exists. A tag with that name exists on the remote and points somewhere else. Git refuses to overwrite it. Do not force the push without reading the later chapter on moving published tags.- Unwanted tags appear on the remote.
--tagspushed every local tag. List local tags withgit tagbefore using it.
Exercise
You created an annotated tag v2.0.0 and a lightweight tag test-build, then ran git push --follow-tags. Which tags reach the remote, assuming the tagged commits were part of the push?
Show answer
Only v2.0.0. The --follow-tags option sends annotated tags that point into the pushed commits, and test-build is lightweight, so it stays local.