Sub topic 8
Deleting Local and Remote Tags
This chapter assumes you can create and push tags, and that you know a remote such as GitHub keeps its own copy of the repository. Deleting a tag is a common cleanup step, for example after you tag the wrong commit or a typo creates a name like v1.0.O. The main point to learn is that the local and remote copies of a tag are separate, so you delete each one on its own.
Two copies, two deletions
When you push a tag, Git copies the reference to the remote. After that, your repository and the remote each hold a tag with the same name. Deleting one does not touch the other.
Delete the local tag
Use -d with the tag name:
git tag -d v1.1.0Git prints the hash the tag pointed to, such as Deleted tag 'v1.1.0' (was 4f2a9c1). Keep that hash. If you deleted the wrong tag, you can recreate it with git tag v1.1.0 4f2a9c1 for a lightweight tag. An annotated tag would need its message again.
Deleting a tag never deletes the commit it pointed to. Only the name is removed.
Delete the remote tag
Removing the local tag does not change the remote. Delete the remote copy with a push:
git push origin --delete v1.1.0An equivalent older form pushes an empty value to the tag reference: git push origin :refs/tags/v1.1.0. Both do the same thing.
You can confirm the result by listing the tags on the remote:
git ls-remote --tags originClean up other copies
Other people who already fetched the tag keep it locally. Deleting the tag on the remote does not remove it from their clones, and a later git push --tags from one of them can put it back. Tell your team when you remove a published tag, and ask them to run git tag -d on their side.
Tags that have a GitHub Release
Deleting a tag that has a published GitHub Release has a side effect. The release turns into a draft, because a release needs a tag to point to. Delete the release itself first if you want it gone:
gh release delete v1.1.0
git push origin --delete v1.1.0Adding --cleanup-tag to gh release delete removes the tag in the same step.
Common errors
| Message | Cause | Fix |
|---|---|---|
| `error: tag 'v1.1.0' not found.` | The name is misspelled or the tag exists only on the remote. | Check `git tag -l`, then delete on the remote. |
| `error: unable to delete 'v1.1.0': remote ref does not exist` | The remote tag is already gone. | Run `git ls-remote --tags origin` to check. |
| The tag returns after you deleted it. | Another clone pushed it again. | Delete it in that clone too. |
Exercise
You created and pushed the tag v2.0.0 on the wrong commit. Which two commands remove it from your clone and from the remote origin?
Show answer
Run git tag -d v2.0.0 to delete the local tag, then git push origin --delete v2.0.0 to delete the remote tag. Each command affects only one copy.