In the last tutorial about tags in Git, we learned what a tag in Git is and how it is essential for software development. In the later sections of that tutorial, we created a few tags and then pushed them to the remote. These were the initial steps involving basic operations in tags. In this tutorial, we will do just the contrary. We will go through:
- How to Delete Tags in Git?
- How to Delete a Tag in Git from Local Machine?
- Procedure to Delete a Tag from Remote Repository through Local (Git Bash)
- How to Update a Tag?
- How to Update a Tag in Git from Local Machine?
- Procedure to Update a Tag from Remote Repository through Local (Git Bash)
How to Delete Tags In Git?
A tag like anything else is not a permanent entity. It would be confusing and difficult for us if we would not be able to delete the tags, and the reasons for doing so are many. Maybe you wrongly assumed that the execution of the release. Or perhaps you tag a wrong commit and so on. In this section, we will explore how to delete the tags that we just created from our local and remote repository. For both of these operations, however, we will make use of Git bash (i.e., our local machine).
- What are Tags? (Refer Tags In Git)
- How to Create Tags In Git? (Refer Tags In Git)
- How to Push Tags To Remote Repository? (Refer Tags In Git)
We have got two tags, namely v1.0.1 and ongoing. To demonstrate the objectives of this tutorial, I have added two more tags. Additionally, you can do that as an exercise.
Execute the below command to check the tags you just created.
Two new tags have been added successfully by the name v1.1 and v2.0. As a next step, push these tags to the remote repository.
Now that we are all set up, we can proceed to perform operations on these tags.
How to Delete Tags In Git From Local Repository?
Deleting a tag in Git from the local repository is quite simple. To succeed, you should know the name of the tag to delete(or you can use the git tag command to view all the tags).
Execute the following command to delete the tag "ongoing".
git tag -d ongoing
Note: The "d" flag used with
git tag denotes that we are requesting a delete operation.
Git responds with a success message of the deletion of the tag. In addition to this, the hash code of the operation (d3d18bd) is also a part of the Git response.
Heading over to the remote repository account, we still have four tags listed there, whereas in local, we have three (after deletion).
Let's see how we can delete the tags from our remote repository using Git in our local repository.
How To Delete Tags From Remote Repository Using Git?
Now that we have deleted tags from our local repository, the next step is to reflect this change on our remote repository. The idea is that everyone can sync with it. Moreover, the user can delete manually from GitHub by going to his repository and pressing the delete button. We will discuss this in Tags In GitHub tutorial, where the tutorial will brief you about the various options and importance of Tags concerning GitHub.
To delete the tags in your remote repository from the local repository, enter the following command.
git push origin :<tag_name>
Note: Do remember the space between origin and ":" The command won't work otherwise.
This command constitutes of three components:
- git push: Git push commands git to push our changes to remote (Refer Git Push).
- origin: Origin is the name of the alias we created for our remote repository.
- ongoing: This is the name of the tag, the changes of which we are pushing over to remote.
Execute the command to see the response.
Git responds with a positive message. Refresh your "Tags" page on GitHub to cross-check.
Alright! The operation is successful, and this is how we can delete the tags in the remote and local repository using Git.
In addition to deletion, which is a general operation, the need to update the tag will also arise very often while working in Git. The next section will brief you about the same.
How to Update Tags In Git?
While working in a project in Git, we often have to delete and update stuff. We are required to delete/update branches or delete/update files etc. Similar to this, sometimes, we are required to update the tags in Git. Updating a tag will take your tag to another commit. For example, we can update the tag v1.1 to another commit depicting that the stable Version1.1. of the software is till the new commit now. There can be any number of reasons to perform the update. This section will walk you through the steps of updating the tags in the local machine and reflecting those changes to your remote repository. We will make use of Git for both of these operations.
How to Update Tags In Git In Local Repository?
To update your tags in the local repository, create a new commit above the tagged commit. For example, see in the below image:
The commit with the tag v2.0 is the last commit. There is no commit above that, and the HEAD points to the same commit as we see. Let's make some changes to the ReadME file and commit them with a commit message (Refer First Commit In Git).
Once you finish this, the Git log will look like this:
So, we intend to update the tag v2.0 from 9e81b61 to 6b405e5.
To achieve this objective, execute the following command.
git tag -f <tag_name_to_update> <hash_code_new_commit>
<hash_code> option can be left blank, which will update the tag to the HEAD pointing commit automatically. Moreover, for any other commit, you need to use the hash code.
Execute the command to update the tag successfully.
Cross-check by showing the Git Log once again.
Great! Our local repository is updated. Let's now update our remote repository.
How to Update Tags In Git In Remote Repository?
Similar to deletion, we need to update our operation of updating to the remote repository. As we pushed our creation of tags (Tags In Git), let's see if the same command works or not.
git push origin v2.0
The response constitutes three lines.
- ! [rejected]: This denotes the overall result of the command execution.
- error: Failed to push some refs: This line shows that some of the references were unable to be synchronized with the remote repository.
- hint: Updates rejected as tag already exists: This line helps us analyze that the command we are using is for newly created tags only.
Therefore, we need some other method to force our way onto the remote repository.
To push the updated tag onto the remote repository forcefully, execute the following command:
git push --force origin v2.0
Note: Instead of going this way, you can also achieve the same results by first deleting the commit and then creating a new tag (with the same name) with the intended commit. After this, pushing the tag to the remote repository will do the needful.
Moreover, the change in hash code denotes that we have successfully updated the tag in our remote repository.
We can confirm this by looking over at the remote repository (verifying the hash codes).
There you go. We have our updated tag on the remote repository as well.
To conclude, tags are very crucial in project development involving Git. They help us in labeling the various points in the development/production and release cycles. Additionally, through this label, it becomes easier for us to know at which point stands the different releases of the project. While this covers the part of Git, tags can reveal much more information on GitHub. In other words, they take a special place inside the GitHub components. I am excited to show you all this in GitHub. I hope you are too!!