git

Git Tags Continued - Share, Delete and Checkout

ello

Ayush Poddar

Posted on August 16, 2023

Git Tags Continued - Share, Delete and Checkout

In the previous post introducing Git tags, we learnt what Git tags are, how they are created and how we can view the existing tags. By default, when we run git push, Git does not push the tags you have created to the remote server (eg: Github). We need to be explicit in our intention when we want to push the tags to the remote server.

This post will be a continuation of the previous post, discussing on the following points:

  • How to push tags to a remote server
  • How to delete tags (locally and in the remote server)
  • Create branches based off a tag

How to push tags to a remote server

As already mentioned, merely running git push won't push the tags. In order to push tags to the remote server, we need to use the --tags flag. Hence the command would be:

git push --tags

# In case you want to push to a different origin
git push <remote> --tags
Enter fullscreen mode Exit fullscreen mode

Using the --tags flag will push both annotated and lightweight tags. If you want to push the annotated tags only, then you can use the --follow-tags flag instead.

There is no way to push just the lightweight tags.

How to delete a tag

The command to delete a tag in your local Git repository is:

git tag -d <tagname>
Enter fullscreen mode Exit fullscreen mode

Deleting the tag in the remote server

If the tag has been pushed to the remote server before deleting it in the local Git repository, you need to run another explicit command to delete the tag from the remote server too. The command is:

git push <remote> --delete <tagname>

# Example
git push origin --delete v1.0.1
Enter fullscreen mode Exit fullscreen mode

There is another way to delete a tag in the remote server. The command is:

git push <remote> :refs/tags/<tagname>

The explanation of this command is: Push the part before the colon (:) to the remote tag named <tagname>. In this case, the part before the colon (:) is null.

How to create a branch based off a tag

The general command to create a branch is:

git checkout -b <newBranch> <reference>
Enter fullscreen mode Exit fullscreen mode

where reference can be any valid Git object. Using this command, a new branch based off a tag can be created using:

git checkout -b <newBranch> <tagname>
Enter fullscreen mode Exit fullscreen mode

If you are looking to just explore the code that is versioned by the tag, you can also directly checkout the tag using: git checkout <tagname>. Using this command puts your repository in detached HEAD state. You can use the link provided to learn more about it. I will also be exploring it in a future post of mine.

TL;DR of detached HEAD: In "detached HEAD", if you make some changes and commit them, the changes will not belong to any branch and will be lost. The only way to reach the commit would be by using its commit hash.

Final words

My hope is that by now, you are well equipped with dealing with the basics of Git tags, and are ready to tackle any new concepts that are thrown your way. Again, I also recommend reading my first post on Git tags.

References

💖 💪 🙅 🚩
ello
Ayush Poddar

Posted on August 16, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related