{"id":1736,"date":"2021-12-31T12:12:08","date_gmt":"2021-12-31T18:12:08","guid":{"rendered":"https:\/\/www.becomebetterprogrammer.com\/?p=1736"},"modified":"2022-04-25T09:37:16","modified_gmt":"2022-04-25T14:37:16","slug":"git-alias","status":"publish","type":"post","link":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-alias\/","title":{"rendered":"The Complete Guide to Git Alias: Shorcuts to Be Efficient"},"content":{"rendered":"\n<p>Programmers understand the value of using git for version control and how it plays an important part in the development of software on a daily basis. However, there are git commands used frequently. It is not a secret for programmers to constantly look for ways to optimize processes, make things easier, and also become more productive. Those who want to become more efficient at git workflow should look into learning how to use git alias, which we will explain in this article.<\/p>\n\n\n\n<p><strong>Git alias is an alternative tool git has to generate shortcuts or custom commands for frequently used commands<\/strong>. For instance, as you work on different features it is common to checkout different branches using the <code>git checkout<\/code> command. By setting an alias to the <code>checkout<\/code> command, we could shortcut this command something like <code>git co<\/code> or <code>git cout<\/code>.<\/p>\n\n\n\n<p>Learning how to use git aliases means remembering how to use more git commands, which would mean something else to remember to the already extensive list of things programmers have to keep on their brain. Luckily, using git aliases is rather simple and straightforward, which we will explain in this article and show you where the aliases are located, how to update and delete an alias, how to see all the git aliases, and why you should start using git aliases more often.  <\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<\/div><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-alias\/#How_to_Create_a_Git_Alias\" >How to Create a Git Alias<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-alias\/#Where_is_my_Git_Alias_Stored\" >Where is my Git Alias Stored?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-alias\/#How_to_List_Git_Aliases\" >How to List Git Aliases<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-alias\/#How_to_Update_a_Git_Alias\" >How to Update a Git Alias<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-alias\/#How_to_Delete_Git_Aliases\" >How to Delete Git Aliases<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-alias\/#Why_You_Should_Start_Using_Git_Aliases\" >Why You Should Start Using Git Aliases<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-alias\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-alias\/#Interested_in_Learning_more_about_Git\" >Interested in Learning more about Git?<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Create_a_Git_Alias\"><\/span>How to Create a Git Alias<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can set up a new git alias using the <code>git config<\/code> command followed by the keyword <code>alias<\/code>. Here is the syntax to generate an alias:<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>git config --global alias.&lt;alias&gt; &lt;commands&gt;<\/code><\/pre>\n\n\n\n<p>Where <code>&lt;alias&gt;<\/code> is the name of the alias or shortcut you want to assign, and <code>&lt;commands&gt;<\/code> is the command to execute whenever the alias is called. For instance, if we want to create an alias <code>s<\/code> for the <code>status<\/code> command, it would look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>git config --global alias.s status<\/code><\/pre>\n\n\n\n<p>Once the alias is created, we can call the alias just like if we were calling any command in git.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>git s<\/code><\/pre>\n\n\n\n<p>There are git commands where you would typically pass an additional value such as the <code>checkout<\/code> command. If we need to checkout from &#8220;other_branch&#8221; to &#8220;master&#8221; branch, you would typically add the name of the branch after the <code>checkout<\/code> keyword.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>git checkout master<\/code><\/pre>\n\n\n\n<p>Therefore, If you create an alias <code>cout<\/code> for the <code>checkout<\/code> command, like in the following example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>git config --global alias.cout checkout<\/code><\/pre>\n\n\n\n<p>You could still provide additional options when calling the alias <code>cout<\/code> like if you were executing the <code>checkout<\/code> command.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>git cout master<\/code><\/pre>\n\n\n\n<p>In case you want to create an alias that automatically checks out the master branch, make sure to the branch when creating the alias. <\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>git config --global alias.cout-master \"checkout master\"<\/code><\/pre>\n\n\n\n<p>Notice how we are using quotes whenever there are multiple values for the alias being created. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Where_is_my_Git_Alias_Stored\"><\/span>Where is my Git Alias Stored?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Notice in the previous section we used the keyword <code>--global<\/code> to generate the git alias. That means, the alias we generated is stored in the global configuration file. While it is common to create the alias in the global configuration, that doesn&#8217;t mean you have to generate it there all the time.<\/p>\n\n\n\n<p>There are different configuration files you can use:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Global config file using <code>--global<\/code> keyword<\/li><li>System config file using <code>--sytem<\/code> keyword<\/li><li>Repository config file using <code>--local<\/code> keyword<\/li><li>Per Worktree config file using <code>--worktree<\/code> keyword<\/li><li>Any given config file using <code>-f<\/code> or <code>--file &lt;file&gt;<\/code> keyword<\/li><\/ul>\n\n\n\n<p>Hence, we can create the git aliases and store them on different configuration files. For instance, if we want to store the alias <code>st<\/code> for the <code>status<\/code> command in the repository configuration file, you would use the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>git config --local alias.s status<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_List_Git_Aliases\"><\/span>How to List Git Aliases<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are different ways to display the list of git aliases. One approach is to use the <code>--get-regexp<\/code> command to get the value of all configurations containing the word &#8220;alias&#8221; using regex.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>git config --get-regexp alias<\/code><\/pre>\n\n\n\n<p>Another way is to use the <code>--list<\/code> option which will display the list of all the configurations.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>git config --list<\/code><\/pre>\n\n\n\n<p>The only disadvantage of using <code>--list<\/code> is to display configurations that are not aliases such as branch names or the user&#8217;s personal information.<\/p>\n\n\n\n<p>For a more targeted list of aliases, such as listing only global aliases, make sure to provide the keyword of the config file location. For the global config file, we use the <code>--global<\/code> option.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>git config --global --get-regexp alias\ngit config --global --list<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Update_a_Git_Alias\"><\/span>How to Update a Git Alias<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To update a git alias, use the <code>-e<\/code> or <code>--edit<\/code> config option.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>git config --global --edit<\/code><\/pre>\n\n\n\n<p>This will open the configuration file in the terminal in <a href=\"https:\/\/www.vim.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">VIM mode<\/a>. Using the keyboard, find the <code>[alias]<\/code> section and locate the alias you want to update. Update the command assigned to the alias. Once it is updated, press the ESC key. and type <code>:wq<\/code> and press the ENTER key. This will save the changes made in the config file as well as exit VIM mode in the terminal. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Delete_Git_Aliases\"><\/span>How to Delete Git Aliases<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To delete git aliases, use the <code>--unset<\/code> option followed by <code>alias.&lt;alias_name&gt;<\/code>. For instance, if we want to remove the alias <code>s<\/code> as a shortcut of the <code>status<\/code> command, run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>git config --global --unset alias.s<\/code><\/pre>\n\n\n\n<p>We had also created an <code>s<\/code> alias in the local configuration. We can run a similar command to remove it, but with a different config file option.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>git config --local --unset alias.s<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_You_Should_Start_Using_Git_Aliases\"><\/span>Why You Should Start Using Git Aliases<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Thinking about git aliases as adding more complexity to the list of git keywords a programmer needs to remember can be a reason for programmers to stay away from adding shortcuts. However, the reality is the fewer time programmers spent on typing to implement a solution, the more efficient they are at delivering solutions.<\/p>\n\n\n\n<p>Also, there are commands that are not easy to remember, which setting an alias can make it more readable whenever it is needed. For instance, if you need to <a href=\"https:\/\/www.becomebetterprogrammer.com\/git-revert-last-commit\/\">revert to the last commit<\/a>, you can execute the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>git revert HEAD<\/code><\/pre>\n\n\n\n<p>While <code>HEAD<\/code> references the last commit of the current branch, it might not be as readable as if you had a command name like <code>revert-last<\/code>. In that case, we can create an alias to revert to the last commit.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>git config --global alias.revert-last \"revert HEAD\"<\/code><\/pre>\n\n\n\n<p>Now, we can execute an alias command which is user-friendly and easy to understand.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>git revert-last<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Using git aliases is a powerful tool software developers can take advantage of to develop shortcut commands to quickly execute git workflows and spend less time typing, becoming more efficient. It is also possible to create git aliases to provide a more explicit meaning of what a process does, making it easy to understand by only reading the git alias.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Interested_in_Learning_more_about_Git\"><\/span>Interested in Learning more about Git?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>I wrote other articles explaining how to use other git commands, and I thought you might be interested in reading some of them since you are reading this.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.becomebetterprogrammer.com\/git-rebase\/\" title=\"https:\/\/www.becomebetterprogrammer.com\/git-rebase\/\">How to Rebase in Git: Explained Step-by-Step<\/a><\/li><li><a href=\"https:\/\/www.becomebetterprogrammer.com\/git-revert-last-commit\/\">How to Revert the Last Commit Locally and Remote in Git<\/a><\/li><li><a href=\"https:\/\/www.becomebetterprogrammer.com\/git-head\/\">What is Git HEAD? A Practical Guide Explained with Examples<\/a><\/li><li><a href=\"https:\/\/www.becomebetterprogrammer.com\/learn-how-to-use-version-control-with-git-and-github-the-absolute-guide-for-beginners\/\">Learn How to Use Version Control with Git and GitHub: The Absolute Guide for Beginners<\/a><\/li><li><a href=\"https:\/\/www.becomebetterprogrammer.com\/how-to-update-github-personal-access-tokens\/\">How to Update GitHub Personal Access Tokens?<\/a><\/li><\/ul>\n\n\n\n<p class=\"has-text-align-left\"><strong>Did you like this article?<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-left\">Share your thoughts by replying on <a href=\"https:\/\/twitter.com\/bbprogrammer\" target=\"_blank\" rel=\"noreferrer noopener\">Twitter of Become A Better Programmer<\/a> or to <a href=\"https:\/\/twitter.com\/arealesramirez\" target=\"_blank\" rel=\"noreferrer noopener\">my personal Twitter account<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-rich is-provider-twitter wp-block-embed-twitter\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"twitter-tweet\" data-width=\"550\" data-dnt=\"true\"><p lang=\"en\" dir=\"ltr\">Programmers like ways to be faster at doing things, and if you haven&#39;t heard about git alias, today is your lucky day.<br><br>Git alias is a tool in Git to generate shortcuts to repetitive commands. <br><br>And we wrote an article about it! Check it out!<a href=\"https:\/\/t.co\/oImy5fEz9p\">https:\/\/t.co\/oImy5fEz9p<\/a><\/p>&mdash; Become A Better Programmer (@bbprogrammer) <a href=\"https:\/\/twitter.com\/bbprogrammer\/status\/1476980366007312386?ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"noopener\">December 31, 2021<\/a><\/blockquote><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Git alias is a tool in Git to generate shortcuts to repetitive commands. In this guide, you will learn everything necessary to work with git aliases<\/p>\n","protected":false},"author":1,"featured_media":1750,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[28],"tags":[],"class_list":["post-1736","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-git","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts\/1736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/comments?post=1736"}],"version-history":[{"count":5,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts\/1736\/revisions"}],"predecessor-version":[{"id":2469,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts\/1736\/revisions\/2469"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/media\/1750"}],"wp:attachment":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/media?parent=1736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/categories?post=1736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/tags?post=1736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}