{"id":30839,"date":"2022-07-24T14:34:53","date_gmt":"2022-07-24T14:34:53","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=30839"},"modified":"2022-12-23T05:48:02","modified_gmt":"2022-12-23T05:48:02","slug":"git-tutorial-git-references","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/git-tutorial-git-references\/","title":{"rendered":"Git Tutorial: Git References"},"content":{"rendered":"\n<p>Branches, remote-tracking branches, and tags are all references to commits. All references are named with a slash-separated path name starting with &#8220;refs&#8221;; the names we&#8217;ve been using so far are actually shorthand:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1 2 3<\/td><td>&#8211; The branch &#8220;test&#8221; is short for &#8220;refs\/heads\/test&#8221;. &#8211; The tag &#8220;v2.6.18&#8221; is short for &#8220;refs\/tags\/v2.6.18&#8221;. &#8211; &#8220;origin\/master&#8221; is short for &#8220;refs\/remotes\/origin\/master&#8221;.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The full name is occasionally useful if, for example, there ever exists a tag and a branch with the same name.<\/p>\n\n\n\n<p>(Newly created refs are actually stored in the .git\/refs directory, under the path given by their name. However, for efficiency reasons they may also be packed together in a single file; see&nbsp;<a href=\"http:\/\/www.kernel.org\/pub\/software\/scm\/git\/docs\/git-pack-refs.html\" target=\"_blank\" rel=\"noreferrer noopener\">git pack-refs)<\/a>.<\/p>\n\n\n\n<p>As another useful shortcut, the &#8220;HEAD&#8221; of a repository can be referred to just using the name of that repository. So, for example, &#8220;origin&#8221; is usually a shortcut for the HEAD branch in the repository &#8220;origin&#8221;.<\/p>\n\n\n\n<p>For the complete list of paths which git checks for references, and the order it uses to decide which to choose when there are multiple references with the same shorthand name, see the &#8220;SPECIFYING REVISIONS&#8221; section of&nbsp;<a href=\"http:\/\/www.kernel.org\/pub\/software\/scm\/git\/docs\/git-rev-parse.html\" target=\"_blank\" rel=\"noreferrer noopener\">git rev-parse<\/a>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Showing commits unique to a given branch<\/strong><\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Suppose you would like to see all the commits reachable from the branch head named &#8220;master&#8221; but not from any other head in your repository.<\/p>\n\n\n\n<p>We can list all the heads in this repository with\u00a0<a href=\"http:\/\/www.kernel.org\/pub\/software\/scm\/git\/docs\/git-show-ref.html\" target=\"_blank\" rel=\"noreferrer noopener\">git show-ref<\/a>:<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">\n\n$ git show-ref --heads\nbf62196b5e363d73353a9dcf094c59595f3153b7 refs\/heads\/core-tutorial\ndb768d5504c1bb46f63ee9d6e1772bd047e05bf9 refs\/heads\/maint\na07157ac624b2524a059a3414e99f6f44bebc1e7 refs\/heads\/master\n<span class=\"hljs-number\">24<\/span>dbc180ea14dc1aebe09f14c8ecf32010690627 refs\/heads\/tutorial<span class=\"hljs-number\">-2<\/span>\n<span class=\"hljs-number\">1e87486<\/span>ae06626c2f31eaa63d26fc0fd646c8af2 refs\/heads\/tutorial-fixes\nWe can get just the branch-head names, <span class=\"hljs-keyword\">and<\/span> remove <span class=\"hljs-string\">\"master\"<\/span>, with the help of the standard utilities cut <span class=\"hljs-keyword\">and<\/span> grep:\n\n\n$ git show-ref --heads | cut -d<span class=\"hljs-string\">' '<\/span> -f2 | grep -v <span class=\"hljs-string\">'^refs\/heads\/master'<\/span>\nrefs\/heads\/core-tutorial\nrefs\/heads\/maint\nrefs\/heads\/tutorial<span class=\"hljs-number\">-2<\/span>\nrefs\/heads\/tutorial-fixes\n<span class=\"hljs-keyword\">And<\/span> then we can ask to see all the commits reachable from master but not from these other heads:\n\n\n$ gitk master --not $( git show-ref --heads | cut -d<span class=\"hljs-string\">' '<\/span> -f2 |\n                grep -v <span class=\"hljs-string\">'^refs\/heads\/master'<\/span> )\nObviously, endless variations are possible; <span class=\"hljs-keyword\">for<\/span> example, to see all commits reachable from some head but not from any tag in the repository:\n\n\n$ gitk $( git show-ref --heads ) --not  $( git show-ref --tags )\n(See git rev-parse <span class=\"hljs-keyword\">for<\/span> explanations of commit-selecting syntax such <span class=\"hljs-keyword\">as<\/span> --not.)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>","protected":false},"excerpt":{"rendered":"<p>Branches, remote-tracking branches, and tags are all references to commits. All references are named with a slash-separated path name starting with &#8220;refs&#8221;; the names we&#8217;ve been using so far are actually shorthand: 1 2 3 &#8211; The branch &#8220;test&#8221; is short for &#8220;refs\/heads\/test&#8221;. &#8211; The tag &#8220;v2.6.18&#8221; is short for &#8220;refs\/tags\/v2.6.18&#8221;. &#8211; &#8220;origin\/master&#8221; is short&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-30839","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30839","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=30839"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30839\/revisions"}],"predecessor-version":[{"id":30840,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30839\/revisions\/30840"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=30839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=30839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=30839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}