DevOps

How To Delete Git Tag

Learn to delete local and remote Git tags.

Introduction

Creating tags in Git marks particular points in a project’s development history, such as a new version release. While renaming a tag works for correcting tag typos, creating a wrong tag requires cleaning up the repository and deleting the unnecessary tag, both locally and remotely.

In this tutorial, you will learn to delete a local and remote Git tag with examples.

Learn to delete local and remote Git tags.

Prerequisites

Git Delete Local Tag

Local tags are stored only in the local repository, and they are not synced with remote repositories. Make sure to delete unnecessary tags before pushing them to a remote repository.

In the sections below, we show different examples of deleting tags locally.

Delete a Single Tag

The syntax for deleting a tag from the local repository is:

git tag -d [tag_name]

For example, to delete a tag named v1.3, run:

git tag -d v1.3
Deleting a single local Git tag.

The command deletes the tag and outputs the result. If the command outputs an error, make sure you specified the proper tag name and that the tag exists.

For example, trying to delete a non-existing tag gives the following error:

Trying to delete a non-existing tag.

Delete Multiple Local Tags

It is also possible to delete multiple local tags at once by listing them after the command. For example:

git tag -d v1.5 v1.6 v1.8
Deleting multiple local tags.

The command deletes all the specified tags.

Delete All Local Tags

In case you want to start from scratch and delete all local tags, use the following command:

git tag -l | xargs git tag -d
Deleting all local tags in Git.,

Note: Learn to use the xargs Linux command to convert standard input into arguments for another command.

See Existing Tags

Ensure that the tags are deleted by listing all existing tags. Run the following command:

git tag -l
Listing existing Git tags.

The output lists all existing tags and shows that the previously deleted tags no longer exist in the repository.

Git Delete Remote Tag

If you push a wrong tag to a remote repository, it isn’t enough to delete it only locally. When a tag remains in a remote repository, the next pull request restores it in the local repository as well.

There are two ways to delete the tag remotely.

Push an Empty Reference to Remote

Use the following syntax to delete a tag remotely by pushing an empty reference to the remote tag name:

git push [remote_name] :[tag_name]

For example:

delete a tag remotely by pushing an empty reference to the remote tag name

Important: Git has a tag namespace and a branch namespace, allowing a branch and tag to share the same name. If you are using the same name for a branch and a tag, specify the full ref to make sure you delete the tag and not the branch:

git push [remote_name] :refs/tags/[tag_name]

2. Use the Delete Option

Another way to delete a tag is to use the --delete (-d) option. The syntax is:

git push --delete [remote_name] [tag_name]
  • Delete a Single Remote Tag

The following example shows how to delete a single remote tag from the origin repository:

git push --delete origin v1.6
Deleting a single remote Git tag.
  • Delete Multiple Remote Tags

This command also allows you to delete multiple tags at once. The syntax is:

git push --delete [remote_name] [tag1] [tag2] ...

For example:

git push --delete origin v1.8 v1.0
Deleting multiple remote Git tags.

The command deletes the specified remote tags.

  • Delete All Remote Tags

1. To delete all remote tags, first fetch the remote tags by running:

git fetch

2. Use the following syntax to delete all remote tags:

git push [remote_name] --delete $(git tag -l)

For example:

git push origin --delete $(git tag -l)
Deleting all Git tags in a remote repository.

The command deletes all the tags from the specified remote repository.

Conclusion

This tutorial showed different ways to delete a single or multiple tags from the local and remote repository. If you have only mistyped a tag name, it is also possible to rename a tag.

Next, read our article on more basic Git tag actions.

For more Git tag tutorials, read how to checkout a Git tag or learn to manage Git tag releases.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !