{"id":28,"date":"2022-11-09T17:45:17","date_gmt":"2022-11-09T17:45:17","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/awanf\/?p=28"},"modified":"2022-11-09T17:46:11","modified_gmt":"2022-11-09T17:46:11","slug":"git-tricks","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/awanf\/2022\/11\/09\/git-tricks\/","title":{"rendered":"Git Tricks"},"content":{"rendered":"\n<p>Git is confusing <em>af<\/em>.  There I said it.  In my brief experience using developer tools so far it&#8217;s one of the most clunky (albeit powerful) tools out there for software development and something we all have to learn how to use.  Throughout the course of our capstone project I&#8217;ve come across a few tricks\/hacks that might make your life a little easier.<\/p>\n\n\n\n<p class=\"has-text-align-center has-medium-font-size\"><strong><em>Switching back and forth between branches quickly<\/em><\/strong><\/p>\n\n\n\n<p>Oftentimes I find myself checking out some branch, some other branch, then switching back and having to type out all this stuff out.  When your branch names are like <code>utilities\/audio-processor-improvements<\/code> it gets a bit tedious.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"850\" height=\"28\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image.png\" alt=\"\" class=\"wp-image-29\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image.png 850w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-300x10.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-768x25.png 768w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/figure>\n\n\n\n<p>A trick is to use <code><strong>git checkout -<\/strong> <\/code>to switch back and forth between the two last branches quickly.  Example:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"857\" height=\"224\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-1.png\" alt=\"\" class=\"wp-image-30\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-1.png 857w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-1-300x78.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-1-768x201.png 768w\" sizes=\"auto, (max-width: 857px) 100vw, 857px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-center\"><strong>Navigate to the repo home dir<\/strong><\/p>\n\n\n\n<p>This takes your shell to the repo&#8217;s home directory of the working tree which is especially useful in our project&#8217;s monorepo since our services are often deeply nested:<\/p>\n\n\n\n<p><code><strong>cd $(git rev-parse --show-toplevel)<\/strong><\/code><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.discordapp.com\/attachments\/812784662987669534\/1039940500737761320\/image.png\" alt=\"\" \/><\/figure>\n\n\n\n<p>Note: I did not come up with this myself.  See this <a href=\"https:\/\/stackoverflow.com\/a\/957978\/14218394\">SO answer<\/a>.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Using <code>git for-each-ref<\/code><\/strong><\/p>\n\n\n\n<p>As the documentation states, <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Iterate over all refs that match&nbsp;<code>&lt;pattern&gt;<\/code>&nbsp;and show them according to the given&nbsp;<code>&lt;format&gt;<\/code>, after sorting them according to the given set of&nbsp;<code>&lt;key&gt;<\/code>. If&nbsp;<code>&lt;count&gt;<\/code>&nbsp;is given, stop after showing that many refs. The interpolated values in&nbsp;<code>&lt;format&gt;<\/code>&nbsp;can optionally be quoted as string literals in the specified host language allowing their direct evaluation in that language.<\/p><cite>https:\/\/git-scm.com\/docs\/git-for-each-ref<\/cite><\/blockquote>\n\n\n\n<p><\/p>\n\n\n\n<p>Ok well that basically means nothing to me so let&#8217;s work through an example of using this.  Say we want to get an overview of whatever remote branches exist currently.  Use <code>git for-each-ref refs\/remotes<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"883\" height=\"165\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-3.png\" alt=\"\" class=\"wp-image-32\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-3.png 883w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-3-300x56.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-3-768x144.png 768w\" sizes=\"auto, (max-width: 883px) 100vw, 883px\" \/><\/figure>\n\n\n\n<p>Cool that&#8217;s relatively useful, but it would be more useful to know WHEN were these commits that are listed made.  We can do that by using custom <code>--format<\/code> arguments for our command.  Say we want to visualize some of the previous information with the date added in:<\/p>\n\n\n\n<p><code>git for-each-ref refs\/remotes --format='%(committerdate:relative) ---- %(authorname) ---- %(refname:short)'<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"915\" height=\"169\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-4.png\" alt=\"\" class=\"wp-image-33\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-4.png 915w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-4-300x55.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-4-768x142.png 768w\" sizes=\"auto, (max-width: 915px) 100vw, 915px\" \/><\/figure>\n\n\n\n<p>Unfortunately the <code>--format<\/code> flag requires us to specify all the information we want so adding additional information to this command would require us to add it individually.  More options are available in the <a href=\"https:\/\/git-scm.com\/docs\/git-for-each-ref\" data-type=\"URL\" data-id=\"https:\/\/git-scm.com\/docs\/git-for-each-ref\">documentation<\/a>. Finally, we can sort the information ascending or descending (I prefer descending) by adding the <code>--sort<\/code> flag as so as well as viewing only the unmerged branches.<\/p>\n\n\n\n<p><code>git for-each-ref refs\/remotes --sort=-committerdate --format=\"%(committerdate:relative) ---- %(authorname) ---- %(refname:short)\" --no-merged<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"95\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-5-1024x95.png\" alt=\"\" class=\"wp-image-34\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-5-1024x95.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-5-300x28.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-5-768x71.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5914\/files\/2022\/11\/image-5.png 1168w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>There&#8217;s options to add color and styling but that&#8217;s all I have for now!  You can add these commands as aliases to your <code>gitconfig<\/code> or whatever dotfiles you&#8217;re using.  Onto finishing up the project!<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Git is confusing af. There I said it. In my brief experience using developer tools so far it&#8217;s one of the most clunky (albeit powerful) tools out there for software development and something we all have to learn how to use. Throughout the course of our capstone project I&#8217;ve come across a few tricks\/hacks that [&hellip;]<\/p>\n","protected":false},"author":12668,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-28","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/awanf\/wp-json\/wp\/v2\/posts\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/awanf\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/awanf\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/awanf\/wp-json\/wp\/v2\/users\/12668"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/awanf\/wp-json\/wp\/v2\/comments?post=28"}],"version-history":[{"count":3,"href":"https:\/\/blogs.oregonstate.edu\/awanf\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":37,"href":"https:\/\/blogs.oregonstate.edu\/awanf\/wp-json\/wp\/v2\/posts\/28\/revisions\/37"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/awanf\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/awanf\/wp-json\/wp\/v2\/categories?post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/awanf\/wp-json\/wp\/v2\/tags?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}