{"id":66,"date":"2024-11-06T09:15:00","date_gmt":"2024-11-05T22:15:00","guid":{"rendered":"https:\/\/jm.armijo.au\/dev\/?p=66"},"modified":"2024-11-05T00:25:18","modified_gmt":"2024-11-04T13:25:18","slug":"git-aliases","status":"publish","type":"post","link":"https:\/\/jm.armijo.au\/dev\/blog\/2024\/11\/06\/git-aliases\/","title":{"rendered":"Git Aliases"},"content":{"rendered":"\n<p>Today I would like to discuss the advantages of using git aliases, some considerations, and then I will share with you the aliases that I normally use:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">What are Git aliases?<\/h4>\n\n\n\n<p>If you are familiar with git, you should at least know a few of the most popular commands. For example:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">git checkout<\/code><\/span><\/pre>\n\n\n<p>Git provides a way to write an alias, or shortcut, to execute the same command. For example:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">git co<\/code><\/span><\/pre>\n\n\n<h4 class=\"wp-block-heading\">Why using aliases?<\/h4>\n\n\n\n<p>One of the main reasons to use aliases is to type less. In this example, we just save typing 6 characters every time we need to run the command.<\/p>\n\n\n\n<p>I tested this and it took me about 1 second to write the word <code>checkout<\/code>, but only a few milliseconds to type <code>co<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-verse\">The reason for this is that QWERTY keyboards (the most popular keyboard configuration) are designed to type as slowly as possible, so it's not surprising that typing a simple word could take so long.<\/pre>\n\n\n\n<p>Even if you can type faster than me, I&#8217;m sure that you can type <code>co<\/code> a lot quicker than <code>checkout<\/code>. Saving a second may not seem like much, but it has a cumulative effect on commands that are frequently used.<\/p>\n\n\n\n<p>I tend to believe that it&#8217;s less likely to have spelling mistakes in a short command (although I have no proof of this), so if you use a short alias you&#8217;ll probably won&#8217;t need to re-type the command. And even if there&#8217;s a typo, re-writing a shorter command should be faster than typing again the whole thing!<\/p>\n\n\n\n<p>One time, I met an engineer who used the aliases <code>git co<\/code> and <code>git oc<\/code> for the <code>git checkout<\/code> command. She explained to me that she&#8217;d frequently change the order of the letters when typing, so this was her way to ensure that whatever she entered in her terminal would get the desired outcome.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Considerations<\/h4>\n\n\n\n<p>Whenever you create a new git alias you will have to remember it (otherwise it defeats the purpose). For this reason, my advice is to create aliases as you need them, and not just grab a list of aliases that someone else has created.<\/p>\n\n\n\n<p>When I joined a new job some time ago I met someone that knew how powerful git aliases are. Because he wanted to help me, he shared his git aliases with me. I thanked him for his good intentions, but in the end I did not use his list. Why? Because:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li> that list was tailored to his own needs, and<\/li>\n\n\n\n<li>the characters that he used did not mean anything to me, so I really struggled at trying to remember them<\/li>\n<\/ol>\n\n\n\n<p>To me, <code>co<\/code> means <code>checkout<\/code>, but perhaps you have an easier time remembering <code>ck<\/code>, or <code>ct<\/code>, or <code>cho<\/code>? We&#8217;re all different, and our brains work in different ways.<\/p>\n\n\n\n<p>I truly believe that you should use git aliases, and that it should be something that makes sense and adds value to you.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How to setup a git alias<\/h4>\n\n\n\n<p>When setting up a git alias you can specify that they apply to your entire working environment (a.k.a. global), or just on a specific repository. It&#8217;s worth noting that whatever you decide is stored locally (this is not sent to the remote repository), and that you can update it anytime.<\/p>\n\n\n\n<p>This example shows how to set a global alias:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">git<\/span> <span class=\"hljs-selector-tag\">config<\/span> <span class=\"hljs-selector-tag\">--global<\/span> <span class=\"hljs-selector-tag\">alias<\/span><span class=\"hljs-selector-class\">.co<\/span> <span class=\"hljs-selector-tag\">checkout<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And this is how it&#8217;s done on a specific repository.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">cd <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">repository-directory<\/span>&gt;<\/span>\ngit config alias.co checkout<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can find more details in the official git documentation: <a href=\"https:\/\/git-scm.com\/book\/ms\/v2\/Git-Basics-Git-Aliases\">https:\/\/git-scm.com\/book\/ms\/v2\/Git-Basics-Git-Aliases<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">The git aliases I use<\/h4>\n\n\n\n<p>As I mentioned earlier, I strongly believe that everyone should come up with their own aliases. That said, I see no harm in giving some inspiration by sharing the aliases that I normally use:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">git co # instead of git checkout<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">code<\/span>&gt;<\/span>git br # instead of <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">span<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">\"background-color: initial;font-family: inherit;color: var(--wp--preset--color--foreground);font-size: var(--wp--preset--font-size--medium)\"<\/span>&gt;<\/span>git branch<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">span<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">code<\/span>&gt;<\/span>git ci # instead of git commit\ngit st # instead of git status<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>I usually squash my commits before raising a PR. This is something I do so often that I created an alias for it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">git <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">span<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">\"background-color: initial;font-family: inherit;color: var(--wp--preset--color--foreground);font-size: var(--wp--preset--font-size--medium)\"<\/span>&gt;<\/span>sq # instead of git rebase -i<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">span<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since most times I squash commits I just use the last two commits in my branch, I created an alias just for that:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">git sq2 <span class=\"hljs-comment\"># instead of git rebase -i HEAD~2<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><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>\n\n\n<p>I also tend to rename branches quite often (I may publish something on this in the future). Since I could never remember the command, I decided the create an alias that is easy for me to remember:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">git rename # instead of git<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">span<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">\"background-color: initial;font-family: inherit;color: var(--wp--preset--color--foreground);font-size: var(--wp--preset--font-size--medium)\"<\/span>&gt;<\/span> branch -m<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">span<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Also, when I squash my commits I usually need to see a quick list of the commits in a branch. I created an alias for that:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">git ll <span class=\"hljs-comment\"># instead of git log --oneline<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><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>\n\n\n<p>When working with large repositories with hundreds or thousands of contributors, running <code>git pull<\/code> can take <strong><em>a lot<\/em><\/strong> of time, as the command pulls all new branches, not just the latest changes in the current branch. The command to just update the current branch is <code>git pull &lt;origin&gt; &lt;branch_name&gt;<\/code>. As you can guess, I created an alias for that too:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">git po <span class=\"hljs-comment\"># instead of git pull origin<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><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>\n\n\n<h4 class=\"wp-block-heading\">When not to create an alias<\/h4>\n\n\n\n<p>As you may have observed, I have intentionally only created a handful of aliases. This is because these are the commands that I use the most, so it&#8217;s easy for me to remember them.<\/p>\n\n\n\n<p>I don&#8217;t create aliases for commands I don&#8217;t use often, as I would waste a lot of time trying to remember them, which defeats the purpose.<\/p>\n\n\n\n<p>Destructive commands are another thing that I avoid for aliases, particularly <code>git res<\/code>et (and especially with the <code>--hard<\/code> option.) If I make a mistake and use the wrong alias I could accidentally lose hours of work, which is against the idea of using aliases in the first place. In these cases, I prefer to play it safe and spend one or two seconds rather than risking losing some of the work I&#8217;ve done.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Summary<\/h4>\n\n\n\n<p>Git aliases are a nice feature that used appropriately allows us to be more productive. As any tool, we need to learn to use it in our favour to make the most of it.<\/p>\n\n\n\n<p>What git aliases do you use? Do you have a specific use case for an uncommon git alias? Please share it in the comments!<\/p>\n\n\n\n<p>Jose Miguel<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I would like to discuss the advantages of using git aliases, some considerations, and then I will share with you the aliases that I normally use: What are Git aliases? If you are familiar with git, you should at least know a few of the most popular commands. For example: Git provides a way [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":78,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[12,10,11],"class_list":["post-66","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tools","tag-alias","tag-git","tag-version-control"],"_links":{"self":[{"href":"https:\/\/jm.armijo.au\/dev\/wp-json\/wp\/v2\/posts\/66","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jm.armijo.au\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jm.armijo.au\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jm.armijo.au\/dev\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jm.armijo.au\/dev\/wp-json\/wp\/v2\/comments?post=66"}],"version-history":[{"count":15,"href":"https:\/\/jm.armijo.au\/dev\/wp-json\/wp\/v2\/posts\/66\/revisions"}],"predecessor-version":[{"id":99,"href":"https:\/\/jm.armijo.au\/dev\/wp-json\/wp\/v2\/posts\/66\/revisions\/99"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/jm.armijo.au\/dev\/wp-json\/wp\/v2\/media\/78"}],"wp:attachment":[{"href":"https:\/\/jm.armijo.au\/dev\/wp-json\/wp\/v2\/media?parent=66"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jm.armijo.au\/dev\/wp-json\/wp\/v2\/categories?post=66"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jm.armijo.au\/dev\/wp-json\/wp\/v2\/tags?post=66"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}