You can safely delete a git tag if you created one unintentionally.
Delete git tag from local repository
Use the git tag
command with the “-d” option to remove a tag from the local repository.
git tag -d <tag_name>
Let’s say you want to delete tag v1.2.3
, then the following command can be used.
git tag -d v1.2.3
After executing this command, you should see a similar output as below.
Deleted tag 'v1.2.3' (was c96fd77)
You can check if the tag was deleted by viewing all the existing tags in your local repository. To display all tags, you can use the following command.
git tag -l
Delete git tag from remote repository
If you want to delete a tag from a remote repository, you can use the git push
command with the –delete
option.
git push --delete origin <tagname>
Let’s say you want to delete tag v1.2.3
, then the following command can be used.
git push --delete origin v1.2.3
After executing this command, you should see a similar output as below.
To https://github.com/user/repo.git
- [deleted] v1.2.3
If you want to list all git tags in the remote repository, you can do so by executing the following command.
git ls-remote --tags origin
Advanced concepts
Deleting git tags by specifying “refs/tags”
This can be useful if you have a branch with the same name as a tag.
In this case you can use the following command to delete a tag.
git push origin :refs/tags/<tag>
Let’s say you want to delete tag v1.2.3
, then the following command can be used.
git push origin :refs/tags/v1.2.3
What exactly is a git tag?
A git tag is a pointer to a specific commit in the repository. It is used to mark a specific point in the repository history. It is similar to a branch, but it does not change as new commits are added to the repository. A tag name just like a branch name, simply points to one commit.
A key differences between tag name and branch name is that a branch name moves over time. Meaning, as you add new commits, the branch name continues to point to the last commit. On the other hand, a tag name is a static/fixed pointer to a specific commit. It does not move as new commits are added to the repository.
Conclusion
In this article, we learned how to delete git tags from the local and remote repository. We also learned how to list all git tags in local and remote repository.