Javaexercise.com

How To Delete A Remote Tag In Git?

Tags are a way of marking specific points in the history of our projects. Like branches, a tag is a pointer or a reference to a commit point. The most common use case of tags is to mark version releases of a project, like v1.0.1 or v2.1.1.

Tags can exist in our local repo. Or we can push them to the remote repository. Tags present in the remote repository are known as Remote Tags. We will discuss two methods to delete Remote Tags. Let's learn how to delete a Remote Git tag.

Method 1

The Git Push command pushes or uploads changes from the local repository to the remote. We can also use this command to delete remote tags. Use the --delete option with Git Push and mention the remote name and the tag name.

git push --delete <remote-name> <tag>

To delete multiple remote tags at once, we need to mention the name of all the Git Tags.

For example, if we need to delete three remote tags(v1.1, v1.2, and v1.3) from the origin, then we will use the following command.

git push --delete origin v1.1 v1.2 v1.3

Note that the syntax to delete a remote branch is the same as the above command. If a branch and a tag have the same name, Git may delete the Remote Branch instead of the Tag.

git push --delete <remote-name> <branch-name>
git push --delete <remote-name> <tag-name>

Method 2: Branch and Tag with the Same Name

As discussed in the previous section, if a branch and a tag have the same name, we can't use the --delete option.

We can specify the complete reference for the tag to make sure that Git doesn't delete a branch.

git push <remote-name> :refs/tags/tag-name

The original syntax used in the above command is given below.

git push <remote-name> source-ref:destination-ref

The above command pushes the source-reference to the destination-reference of the mentioned remote repository. By not mentioning the source reference, we are effectively replacing the destination reference with nothing. Thus, Git ends up deleting the destination reference.

We can also use the colon(:) syntax to delete multiple remote tags with a single command. Use the code shown below to delete multiple remote tags.

git push <remote-name> :refs/tags/tag-1 :refs/tags/tag-2 :refs/tags/tag-3 

Deleting Local Tags

Git provides the Git Tag command to work with local tags. Use the -d flag with the Git Tag command to delete a Local Tag.

git tag -d <tag-name>

Use the following command to delete multiple local tags at once.

git tag -d <tag-1> <tag-2> <tag-3>

Example

Consider a remote repository with a single master branch, five commits, and two tags - v1.0.1 and v1.0.2.

Remote Repository

A -- B -- C -- D -- E <- master <- HEAD
	 ^			   ^
   v1.0.1		 v1.0.2

We have cloned this repository on our local system. Now, we want to delete the tag v1.0.2 from the remote repo. We have discussed two methods to do so. We don't have any branch named v1.0.2. So both methods work fine. If there is a remote branch with the same name, then use the second method.

Using the first method:

git push --delete origin v1.0.2
Output

To remote-repo-URL
 - [deleted]         v1.0.2

Using the second method:

git push origin :refs/tags/v1.0.2
Output

To remote-repo-URL
 - [deleted]         v1.0.2

The state of our remote repository is shown below. The tag v1.0.2 no longer exists.

Remote Repository

A -- B -- C -- D -- E <- master <- HEAD
	 ^			   
   v1.0.1		

If we wish to delete the local tag as well, we can run the following command.

git tag -d v1.0.2
Output

Deleted tag 'v1.0.2' (was 32ef501)

In the output above, the 32ef501 is a hash value. For lightweight tags, it is the hash of the tagged commit. For annotated tags, this is the hash of the tag itself.

Conclusion

Tags are simple pointers or references. We use them to mark specific points in the history of our project. We need to use the Git Push command to delete a remote tag. We can either use the --delete option. Or we can use colon(:) syntax to avoid accidentally deleting a branch.

Deleting local tags is also pretty straightforward. Use the Git Tag command with the -d flag to delete local tags.