How To Delete Local and Remote Tags on Git
On Git, tags are often used in order to tag specific commits that may be more important than others.
Tags may be used in order to bookmark certain events : releases, bug-fixes or just to add an informative and descriptive note to a commit.
On GitHub, tags are often associated with actual product releases for example.
However, in some cases, you may want to delete Git tags easily locally or remotely.
Delete a local Git tag
In order to delete a local Git tag, use the “git tag” command with the “-d” option.
$ git tag -d <tag_name>
For example, if you wanted to delete a local tag named “v1.0” on your commit list, you would run
$ git tag -d v1.0 Deleted tag 'v1.0' (was 808b598)
If you try to delete a Git tag that does not exist, you will simply be notified that the tag does not exist.
$ git tag -d v2.0 error: tag 'v2.0' not found.
If you want to make sure that tags were correctly deleted, simply list your existing tags using the tag command and the “-l” option.
$ git tag -l <empty>
Delete a remote Git tag
In order to delete a remote Git tag, use the “git push” command with the “–delete” option and specify the tag name.
$ git push --delete origin tagname
Back to the previous example, if you want to delete the remote Git tag named “v1.0”, you would run
$ git push --delete origin v1.0 To https://github.com/SCHKN/repo.git - [deleted] v1.0
To delete a remote Git tag, you can also use the “git push” command and specify the tag name using the refs syntax.
$ git push origin :refs/tags/<tag>
Back to the example, in order to delete a tag named “v1.0”, you would run
$ git push origin :refs/tags/v1.0 To https://github.com/SCHKN/repo.git - [deleted] v1.0
Why should we specify the “refs/tags” instead of just specifying the tagname?
In some cases, your tag may have the same name as your branch.
If you tried to delete your Git tag without specifying the “refs/tags” you would get the following error
$ git push origin :v1.0 error: dst refspec v1.0 matches more than one. error: failed to push some refs to '<repository>'
As a consequence, you need to specify that you are actually trying to delete a Git tag and not a Git repository.
In this tutorial, you learnt how you can easily delete a local and a remote Git tag.
If you are curious about Git, we wrote other tutorials on the subject :
Also if you are interested in software engineering, we have a complete section dedicated to it on the website, have a look!