{"id":8140,"date":"2020-04-29T21:00:59","date_gmt":"2020-04-29T21:00:59","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8140"},"modified":"2020-04-29T21:00:59","modified_gmt":"2020-04-29T21:00:59","slug":"git-tags-using-the-cli","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/git-tags-using-the-cli\/","title":{"rendered":"Git tags using the CLI"},"content":{"rendered":"<p>Creating tags from the command line&#8230;<\/p>\n<p>Tags allows us to store a pointer to the repository at a point in time, these are often used for &#8220;tagging&#8221; a release, but can be used for other purposes.<\/p>\n<p><em>Note: I will use the tag name v1.0.2 here, obviously this should be replaced with the tagname you&#8217;ve used\/assigned.<\/em><\/p>\n<p><strong>Listing your tags<\/strong><\/p>\n<p>To see what tags you currently have on a repository run<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngit tag\r\n<\/pre>\n<p>this will list all your tags. <\/p>\n<p><strong>Creating your tags<\/strong><\/p>\n<p>There&#8217;s a couple of ways to create your tags, the first is the annotated tag<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngit tag -a v1.0.2 -m &quot;Version 1.0.2&quot;\r\n<\/pre>\n<p>Here we use -a (annotate) to create a new tag and -m (add a message). When creating an annotated tag, a message is expected and so you will be prompted to enter a message if you omit -m. Annotated tags store the message (although you could specify an empty message) along with the author of the tag. <\/p>\n<p>Lightweight tags are another way to tag, these tags store no data with the tag (so do not supply -a or -m), for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngit tag v1.0.2\r\n<\/pre>\n<p>These tags store the commit checksum and whereas you&#8217;d tend to use the annotated tag for releases (for example) the lightweight tag might be used simply to label certain commits.<\/p>\n<p>We can also tag by using the commit checksum (the 6a0b83 in the example below), this example uses a lightweight tag <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngit tag v1.0.2 6a0b83\r\n<\/pre>\n<p><strong>Pushing your tags<\/strong><\/p>\n<p>Ofcourse, if we&#8217;re working with others we&#8217;ll want to share our tags, so if we have a remote to push to then we can push the tag using<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngit push origin v1.0.2\r\n<\/pre>\n<p>A simple <em>git push<\/em> does not push our tags, we need to be explicit or we can use the <em>&#8211;tags<\/em> switch to push all tags.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngit push origin --tags\r\n<\/pre>\n<p><strong>Viewing your tag<\/strong><\/p>\n<p>We&#8217;ve seen that <em>git tag<\/em> will list the tags but what about showing us the annotation tag data or what the tag actually refers to, this is where we use<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngit show v1.0.2\r\n<\/pre>\n<p>If it&#8217;s an annotated tag you see the author of the tag and any message along with the last commit within that tag, for the lightweight we simply see the last commit details.<\/p>\n<p>If you&#8217;ve signed your tag (see <strong>Signing tags<\/strong> below) you&#8217;ll also see the GPG signature attached to the tag.<\/p>\n<p><strong>Deleting your tag<\/strong><\/p>\n<p>To delete a tag just use the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngit tag -d v1.0.2\r\n<\/pre>\n<p>If you&#8217;ve pushed your tag to a remote then use the following after the local delete<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngit push origin :refs\/tags\/v1.0.2\r\n<\/pre>\n<p>Or use<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngit push origin --delete v1.0.2\r\n<\/pre>\n<p><strong>Checking out a tag<\/strong><\/p>\n<p>We can checkout the tag just like any branch using<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngit checkout v1.0.2\r\n<\/pre>\n<p>This will put your repository into a &#8220;detached HEAD&#8221; state. You cannot commit to this tag, or to put it another way, if you commit to this tag your commits will not appear within the tag, instead they&#8217;ll only be reachab;e by the commit hash.<\/p>\n<p>If you intention it to make changes to a tag then instead, you need to branch from the tag, i.e.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngit checkout -b v1.0.2-branch v1.0.2\r\n<\/pre>\n<p><strong>Signing tags<\/strong><\/p>\n<p>Tags can be signed using GPG. The purpose of this functionality is to simply ensure that the tag was created by the person we expected. I&#8217;ve not so far needed to sign a tag (or commits for that matter, which can also be signed). However there may be a time when this is useful, so let&#8217;s go through it.<\/p>\n<p>To check if you have a key installed type<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngpg --list-keys\r\n<\/pre>\n<p>If no keys exist it&#8217;ll create the folders needed for creating keys. <\/p>\n<p>To generate a key key run<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngpg --gen-key\r\n<\/pre>\n<p>This will ask for your &#8220;real name&#8221; and &#8220;email address&#8221;, followed by a pass phrase and then this will create your key. Now running <em>gpg &#8211;list-keys<\/em> should list the newly created key.<\/p>\n<p>Next we need to tell git to use our key using the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngit config --global user.signingkey 123456789\r\n<\/pre>\n<p>where 123456789 is replaced by the <em>pub<\/em> string associated with your key (see output from <em>gpg &#8211;list-keys<\/em>).<\/p>\n<p>Now we can sign our tags using<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngit tag -s v1.0.2 -m &quot;Version 1.0.2&quot;\r\n<\/pre>\n<p>We replace -a with -s, this is now a signed, annotated tag.<\/p>\n<p>We&#8217;ll probably want to verify our tag, and we do this using<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngit tag -v v1.0.2\r\n<\/pre>\n<p>If you have the public key installed you&#8217;ll see information about the key, if not then you&#8217;ll get a verification error.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating tags from the command line&#8230; Tags allows us to store a pointer to the repository at a point in time, these are often used for &#8220;tagging&#8221; a release, but can be used for other purposes. Note: I will use the tag name v1.0.2 here, obviously this should be replaced with the tagname you&#8217;ve used\/assigned. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[206],"tags":[],"class_list":["post-8140","post","type-post","status-publish","format-standard","hentry","category-git-2"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8140"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8140\/revisions"}],"predecessor-version":[{"id":8144,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8140\/revisions\/8144"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}