Git tags identify specific commits in a Git repository. They include alphabet letters, numbers, and special characters. Tags are extremely valuable if you wish to quickly find the specific commit as well as gain better knowledge on the development flow. They are also used to specify the starting point of a branch or mark important milestones of the development process.
Tags in Git are unique to the repository in which they are created, and unlike branches, each tag points to only one commit. During the development process, a situation may occur where you accidentally create a tag at a wrong commit and push it to the remote server. In that case, you would naturally want to correct the mistake by deleting the tag in question. To keep your development workflow on the right track, our web development agency Chicago explores ways to instruct Git to delete a remote tag.
What is Git, and how does it work?
Git is one of the most popular scalable version control systems around the globe. It has an extremely rich command set that provides high-level operations and complete access to internals. Git functions in a way that it tracks and records all modifications performed to files in the Git repository giving you the option to revert to a previous version of the file at any time. One of the most valuable features of Git is that it provides seamless collaboration between team members, with each member of the team making changes that are later merged into one source.
What is the Git command line?
Git rests on a set of command-line service programs specifically designed to perform on a Unix-style command-line environment. While some operating systems such as Linux and macOS have built-in command-line terminals, in Windows environments, Git is usually packaged as part of higher-level GUI applications.
What are tags in Git?
Tags allow Git users to, as the name itself suggests, tag particular points in the Git repository history and answer to them later. Once a tag is created, it has no commit history. Git supports two types of tags:
- Annotated tags
- Lightweight tags
Annotated tags are public tags, while lightweight tags are private. Apart from this, the main difference between these two types of tags is the amount of data they can store.
How to command Git to delete a remote tag?
Basically, to instruct Git to delete a remote tag, you need to use the git push command along with the -delete option (or -d flag) and tag name specified.
There are two ways to delete a remote tag in Git. You can either first delete the tag in question from local and afterward push the changes to the remote, or you can delete the tag directly from the remote. Let’s explore both options.
Note that we’ll suppose that the name of the remote is origin by default.
Option #1: Delete local Git tag and push changes to remote
Let’s start by listing all the tags in order to identify the one we wish to delete. We’ll do this with the help of the git tag command along with the -l option:
git tag -l
We’ll be presented with a list of tags that, depending on the way you are naming your tags, may look something like this:
Now, let’s say you wish to delete the tag named v2.5. To do so, simply type:
git tag -d v2.5
After this command, Git will display the following message to show which tag is deleted:
Deleted tag ‘v2.5’ (was aea93f1727)
We can confirm that our tag has indeed been deleted by once again running the git tag -l command:
git tag -l
As you can notice, v.2.5 tag is no longer on the list. Once the tag is removed from the local, we need to tell Git to delete the remote tag. We’ll do this by using the command below:
git push origin :refs/tags/v2.5
Why did we use the refs syntax?
It may sometimes happen that the tag you wish to delete has the same name as your branch. Since the command for deleting a tag is the same as the one for deleting a branch, you need to use the refs/tags syntax instead of simply specifying the tag name in order to tell Git that it is the tag you are attempting to delete, not the branch.
So, let’s assume that we also do have a branch named v2.5. If we were to attempt to delete a tag by that name without using the refs/tags, we would get the following error:
git push origin :v2.5
error: dst refspec v2.5 matches more than one.
error: failed to push some refs to ‘<repository>’
Option #2: Delete the tag directly from the remote
To delete the tag in question directly from the remote, we’ll use the git push command along with the -delete option (or -d flag) and tag name specified. So, if we were to follow our previous example and instruct Git to delete the remote tag named v2.5, we would run the following:
git push –delete origin v2.5
Deleting a non-existing Git tag
Let’s start by listing our tags:
git tag -l
As you can see, we have no tag named v4.0. So, what would happen if we attempted to delete the tag by that name?
git tag -d v4.0
If we were to run this command, Git would notify us that the tag does not exist:
error: tag ‘v4.0’ not found.
Tags are an extremely important part of any Git repository since they provide additional information regarding our project’s history. However, we may need to delete a tag every so often due to a typo, or maybe we accidentally created a tag at a wrong commit and pushed it to the remote server. In today’s article, we have discussed two methods of deleting remote tags. We’ve talked about how to delete a tag locally and push changes to the remote, as well as how to delete a tag directly from the remote.