Sub topic 9
Moving Published Tags and Why to Avoid It
This chapter assumes you can create, push and delete tags, as covered in the earlier chapters. It explains what happens when you move a tag that others already have, and what to do instead.
Why tags are not meant to move
A tag is a name that points at one commit. Nothing in Git stops you from pointing the same name at a different commit. That is called moving a tag, or retagging. On your own machine, before you push, this is harmless. After you push, it becomes a problem.
People and tools treat a version tag as a promise. If v1.1.0 meant one commit yesterday, it should mean the same commit today. Build systems, package managers, deployment scripts and release notes all rely on that.
How a tag gets moved
You can move a tag with --force, then push it with --force:
# Point v1.1.0 at the current commit instead of the old one
git tag -f -a v1.1.0 -m "Release 1.1.0"
# Overwrite the tag on the remote
git push --force origin v1.1.0Without --force, Git refuses both steps and tells you the tag already exists.
What goes wrong
Git does not update a tag that you already have when you run git fetch or git pull. Git assumes tags do not change.
- Teammates who fetched the old tag keep pointing at the old commit. They see no error.
- Two people can build "version 1.1.0" from different code.
- A GitHub Release linked to the tag may show files and notes that no longer match the tagged code.
- Anyone who pinned the old commit hash, for example in a deployment file, now disagrees with the tag.
What to do instead
If a release was wrong, publish a new version rather than rewriting the old one.
- Fix the problem in a new commit.
- Tag it with the next version, such as
v1.1.1. - Create a new release, and note in its description which version it replaces.
Never-published tags are different. If you tagged the wrong commit and have not pushed, delete it locally with git tag -d and tag again.
When you must move a tag
Sometimes a tag is pushed by mistake within minutes, and nobody could have used it. If you decide to move it:
- Tell everyone who may have fetched it.
- Delete the tag locally and remote, then create it again.
- Ask others to run
git tag -d v1.1.0andgit fetch --tagsso they receive the new one.
Exercise
A teammate fetched tag v2.0.0 yesterday. Today you force-push v2.0.0 to a different commit. They run git pull. Which commit does their local v2.0.0 point to, and what is the safer fix?
Show answer
Their local tag still points to the old commit, because git pull does not overwrite an existing tag. The safer fix is to leave v2.0.0 alone, commit the correction, and publish a new tag such as v2.0.1.