DEV Community

Cover image for Git Remove Local and Remote Tag : The Final Guide
ZigRazor
ZigRazor

Posted on

Git Remove Local and Remote Tag : The Final Guide

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>
Enter fullscreen mode Exit fullscreen mode

For example, if you wanted to delete a local tag named “v0.1” on your commit list, you would run

git tag -d v0.1
Enter fullscreen mode Exit fullscreen mode

The output will be:

Deleted tag 'v0.1' (was 88f2a35)
Enter fullscreen mode Exit fullscreen mode

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 v0.2
Enter fullscreen mode Exit fullscreen mode

the output will be:

error: tag 'v0.2' not found.
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Back to the previous example, if you want to delete the remote Git tag named “v0.1”, you would run

git push --delete origin v0.1
Enter fullscreen mode Exit fullscreen mode

the output will be:

To https://github.com/ZigRazor/repo.git
 - [deleted]         v0.1
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Back to the example, in order to delete a tag named “v0.1”, you would run

git push origin :refs/tags/v0.1
Enter fullscreen mode Exit fullscreen mode

the output will be:

To https://github.com/ZigRazor/repo.git
 - [deleted]         v0.1
Enter fullscreen mode Exit fullscreen mode

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>'
Enter fullscreen mode Exit fullscreen mode

As a consequence, you need to specify that you are actually trying to delete a Git tag and not a Git repository.

Conclusion

In this tutorial, you learnt how you can easily delete a local and a remote Git tag.


For More "The Final Guide" see the Index Page

Discussion (0)