{"id":1238,"date":"2022-06-21T19:12:25","date_gmt":"2022-06-21T19:12:25","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=1238"},"modified":"2022-12-20T05:47:08","modified_gmt":"2022-12-20T05:47:08","slug":"git-tag-all-you-need-to-know","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/git-tag-all-you-need-to-know\/","title":{"rendered":"Git Tag &#8211; All You Need to Know"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">What is a git tag?<\/h4>\n\n\n\n<p>A Git Tag is just a label or name given to a particular commit in git history. It is like a branch which does not change.  Unlike branches, tags, after being created, have no further history of commits.  &#8216;<\/p>\n\n\n\n<p> Tag is usually used to mark release points (eg. v1.0.1, v1.0.2, etc.). But it can be used to mark any important point so that we can refer to it in future if needed.<\/p>\n\n\n\n<p>Say, you are working on dev branch in some project. Once the initial version of project is ready to demo, you can create a tag (say v1.0.0) in dev branch. By default, git tag will create a tag on the commit that HEAD is referring. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>HEAD<\/strong> is nothing but a reference\/pointer that points to the latest commit in the current branch.<\/p><\/blockquote>\n\n\n\n<p>Now, some new requirements came and you decided to create a new branch (say, feature\/sso_login) out of dev branch and start working on that branch. Once the requirements are done, you will have to merge the  feature\/sso_login branch with dev branch so that dev branch will have all the latest code. After merging is done, you can create another tag (say, v1.1.0) in dev branch.<\/p>\n\n\n\n<p>Now, if you checkout the tag v1.0.0, you will just have the initial code without sso login feature. This is where tag plays important role. If you would have not created v1.0.0 tag in dev branch, after merging feature\/sso_login branch with dev, you would never have been able to checkout the initial version of code. This is why, we should always create tag before and after merging other branches to main\/base branch. In our case, dev is the main\/base branch.<\/p>\n\n\n\n<p>We can also create tags (by date like release-12-01-2022, release-20-01-2-22 and so on) whenever we are deploying code to production. In this way, we can keep track of deployments. In emergency or due to some reason, if we need to revert back to previous deployment, we can simply checkout the previous tag and then build and deploy.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Types of Tags<\/h4>\n\n\n\n<p>There are two types of tags in git &#8211;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Lightweight Tags<\/li><li>Annotated Tags<\/li><\/ol>\n\n\n\n<p>Lightweight tags and Annotated tags differ in the amount of accompanying metadata they store.  Annotated tags store extra metadata such as&nbsp;<strong><em>the tagger name, email, and date<\/em>.<\/strong>&nbsp;This is important data for a public release.&nbsp; <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Create Tag<\/h4>\n\n\n\n<p>Git tag can be created using the <code>git tag<\/code> command. Please not that once you create a tag, you will have to push it to remote repository.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Create lightweight tag\ngit tag &lt;tag-name>\n\nExample: \ngit tag v1.0.0\n\n# Create annotated tag\ngit tag -a &lt;tag-name>\n\nExample:\ngit tag -a v1.0.0\n\n# Create annotated tag with message\ngit tag -a &lt;tag-name> -m \"message\"\n\nExample:\ngit tag -a v1.0.0 -m \"Initial Release, Code Frozen\"\n\n<\/code><\/pre>\n\n\n\n<p>Tags are recorded in git commit history. After creating a tag, run <code>git log<\/code> command to view the tag in git history.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git log --oneline<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">View Tags Or List All Stored Tags<\/h4>\n\n\n\n<p><code>git tag<\/code> command below to list all the tags that are stored in the current repository.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># List all tags\ngit tag\n\n# Output will be similar to the below one:\nv1.0.0\nv1.1.0\n\n# List tags with message\ngit tag -n\n\n# Output will be similar to the below one:\nv1.0.0       Initial Release, Code Frozen\nv1.1.0       SSO Implementation\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">View Specific Tag<\/h4>\n\n\n\n<p>git show command is used to view a particular tag. As mentioned earlier, lightweight tags will have less details (meta data) compared to annotated tags.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git show &lt;tag-name>\n\nExample:\ngit show v1.0.0\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Search Tag<\/h4>\n\n\n\n<p>If you want to search and list tags with &#8220;beta&#8221; in their name, use the following command-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git tag -l '*beta*'<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"646b\">Push Tags To remote<\/h4>\n\n\n\n<p>When we create tag, it is created and stored locally in out PC. So, it is imperative to push tags to remote repository.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Push all tags to remote repository\ngit push origin --tags\n\n# Push a particular tag to remote repository\ngit push origin &lt;tag-name>\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Checkout Tag<\/h4>\n\n\n\n<p>We can checkout a tag the way we checkout a branch. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git checkout &lt;tag-name>\ngit checkout v1.0.0\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Create Branch From Tag<\/h4>\n\n\n\n<p>We can create a branch from a tag using <code>switch<\/code> command as shown below-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git switch -c &lt;tag-name> &lt;branch-name>\n\n# Say, create a branch and implement gmail signin on top of v1.1.0 code\ngit switch -c v1.1.0 gmail_signin\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Delete Tag<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code># Delete Local tag\ngit tag -d &lt;tag-name>\ngit tag -d v1.1.0\n\n# Delete remote tag:\ngit push --delete origin &lt;tag-name>\ngit push --delete origin v1.1.0\n<\/code><\/pre>\n\n\n\n<p>We can also delete remote tags by using refs syntax-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Delete remote tag using refs syntax\ngit push origin :refs\/tags\/&lt;tag-name>\ngit push origin :refs\/tags\/v1.1.0\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Diff Between Two Tags<\/h4>\n\n\n\n<p><code>git diff<\/code> command is used to find out the differences between two tags-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git diff &lt;tag-name-1> &lt;tag-name-2>\ngit diff v1.0.0 v1.1.0\n<\/code><\/pre>\n\n\n\n<p>This is show all the modifications that has been done in v1.1.0 as compared to v1.0.0. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Summary<\/h4>\n\n\n\n<p>Basic steps to create a tag and push it to remote repository-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># 1. List existing tags\ngit tag -n\n\n# 2. Create tag\ngit tag -a &lt;tag-name> -m \"message\"\n\n# 3. Push a particular tag to remote repository\ngit push origin &lt;tag-name>\n\n# 4. Check git log to verify if the tag is recorded\ngit log --oneline\n<\/code><\/pre>\n\n\n\n<p>Hope this article helps. Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is a git tag? A Git Tag is just a label or name given to a particular commit in git history. It is like a branch which does not&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":1260,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[266,265],"tags":[264,276,274,277,275,273,272],"class_list":["post-1238","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-git","category-vcs","tag-git","tag-git-checkout","tag-git-diff","tag-git-log","tag-git-switch","tag-git-tag","tag-tags","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=1238"}],"version-history":[{"count":23,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1238\/revisions"}],"predecessor-version":[{"id":1262,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1238\/revisions\/1262"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media\/1260"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=1238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=1238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=1238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}