“Attitude is like a price tag; it decides your value.” It is quite a typical quote and truly depicts the meaning of the word “tag.” Tag is not an alien word for any of us. A tag is a label that works as an additional identifier (or an identifier if there aren’t any). So if I want to tag the great scientist Dr. APJ Abdul Kalam as “missile man” then that is the identifier for him. Next time someone says, “missile man,” it will imply what is meant by that. Let’s see how this analogy is related to Git tags…
A tag has a similar meaning in Git and GitHub. Tags help in identifying different commits that are important enough to be recognized or be noticed. For example, tagging a commit with release version 3.0 means that commit was the final commit before the launch of the 3.0 version of the software. Moreover, it is convenient and easy. GitHub provides a whole different section to look at the tags and analyze them there. In this tutorial, we will look at the same highlights:
- What Are Tags In Git?
- How to Create Tags in Git?
- How To View The Tags In Git?
- Procedure to Push The Tags To Remote Repository
- How To View Tags In GitHub?
What Are Tags In Git?
Tags do not appear significant, but they stand as one of the most robust features in Git and GitHub. Although Git limits the feature to just marking essential events or milestones in the project, GitHub has extended this feature to the release versions of the software. We will see this in the later sections of this post.
Tags might sound a little different as a concept. Tags in Git are the reference points in the Git history that denotes special events. It is not a rule to tag the releases. You can tag any commit for any purpose you want. In addition to that, no matter how much time we spend on the project, any new member can look at the git log and identify unique points in the project’s timeline through Git. For releases, GitHub provides another option. So let’s see how we can tag in Git and push the same local tags to the remote repository located in the remote repository.
How To Create Tags in Git?
Creating the tags in Git is very simple. But, the user must be familiar with the Git Log beforehand. So, if the git log seems new to you, it is advised to read the tutorial about the git log command first. Tags can be associated with either the commit to which the HEAD points or a specific commit that the user likes. In this section, we will see both of them.
How To add a Tag In Git?
1.Open Git Bash in the working directory.
2. Check if you have a clean working directory.
3. Execute the following command to view the commits:
git log –oneline
4. We can now create a tag onto any of these commits. Let’s tag the last commit on the dev branch by executing the following command:
git tag ongoing dev
Note: A commit to dev branch will not reflect in any other branch. It is personal to dev command only.
5. Now you can check yourself by executing git log –oneline command that the tag creation is successful.
Although this command will tag the last commit on the branch dev, you can also tag with a specific commit in Git.
How To Tag Specific Commit In Git?
For tagging a specific commit, we will make use of the hash code of that particular commit. To list out the commits, let’s execute the git log –oneline command once again.
The commit is f030ee9. Let’s tag it with the following command:
git tag -a <tag_name> -m “<Message_for_commit>” <commit_hash_code>
Note: “-a” denotes the annotated tags, which in layman terms means tags that are specific and not generalized. –m is the flag to tell that the sentence that is followed is the commit message.
Here we go! We have tagged the commit successfully. If the user intends to view the tags and commits, they can also use the git log –oneline command like as shown below:
Note: Viewing tags like this sometimes depends on the client and operating systems. Sometimes different OS do not show the tag in one line command for previous versions also. So, it is better to use git alias.
How To View Tags In Git?
The user can view all the tags in the repository by the following command:
So, we are all set now by tagging the commits on local machines. But, these commits are not yet visible on our remote repository on GitHub. Therefore, let’s push these tags and see where they are available in GitHub.
How to Push Tags to GitHub?
Pushing the tags is similar to pushing anything on the remote repository. Follow these steps to push the tags on the remote repository:
1. First, Open Git Bash in the local working directory.
2. Ensure that there are no changes on the remote that are yet to sync up with the local machine. We can achieve this through the git pull command. (Refer Git Pull ).
3. Git does not push the tags by the normal git push command. If we use git push as it is, we get the following response:
4. Git says that everything up-to-date, which means there was nothing to push, which is not the case. To push the tags into the remote repository type in the following command:
git push –tags
5. Press enter to execute the command.
6. Git says that both of our tags: ongoing and v1.0.1, are pushed successfully. Let’s verify these on the GitHub repository.
How to View Tags in GitHub?
To verify the tags that we created in the local repository, visit your GitHub account.
Go to the Git Repository Home page and visit the releases tab on the home page.
In this panel, all the tags will be visible that we just created in our local repository.
This way, you can push all the local tags on to your remote repository and view them in your account. But, we still have to work with the tags. Notice that GitHub named the tab as Releases and tag is just a tab inside Releases. We will see this in the later tutorial. We will continue the discussion in the next tutorial, where we will try to delete and play around with the tags on GitHub and understand the term releases concerning GitHub.
Common Git Tags Questions
Can I add Tags directly through GitHub?
Yes, a user can directly tag the commit through GitHub without making use of Git. It can be done through the releases tab and drafting a new release. It is discussed in detail in Tags in the GitHub tutorial.
Are Git Tags Immutable?
Yes, Git Tags are immutable, and once created, they cannot change. You need to delete the tag and recreate it, although the tag can update to another commit.
Can we tag a branch in Git?
No, tags can only be a reference to commits. Branches cannot tag in Git.
Is Git Tag and Release the same?
No, Tag is a Git concept, whereas Release is specific to GitHub. The usage of Release is to publish the release notes and binary files along with other information. The usage of Tags happens commonly for tagging the release commits, but their usage is not limited to this.