<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Brian Riehman</title>
    <description>The latest articles on DEV Community by Brian Riehman (@briehman).</description>
    <link>https://dev.to/briehman</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F384324%2Fced997f6-6633-4010-92ae-8a551e7366e5.jpeg</url>
      <title>DEV Community: Brian Riehman</title>
      <link>https://dev.to/briehman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/briehman"/>
    <language>en</language>
    <item>
      <title>Using Git Aliases and Custom Scripts to Work Faster</title>
      <dc:creator>Brian Riehman</dc:creator>
      <pubDate>Tue, 12 May 2020 02:06:34 +0000</pubDate>
      <link>https://dev.to/briehman/using-git-aliases-and-custom-scripts-to-work-faster-18no</link>
      <guid>https://dev.to/briehman/using-git-aliases-and-custom-scripts-to-work-faster-18no</guid>
      <description>&lt;p&gt;Configuration is one of Git's greatest strengths. It can be&lt;br&gt;
tweaked to work exactly as you need. Git has multiple features&lt;br&gt;
to help you work in the way that is best for you.&lt;/p&gt;

&lt;p&gt;One of the most common custom configuration methods is creating&lt;br&gt;
aliases.  These are useful for command abbreviations,&lt;br&gt;
correcting common typos, and composing commands. Git also&lt;br&gt;
supports writing your own custom commands that can also be&lt;br&gt;
leveraged with aliases to create some seriously powerful&lt;br&gt;
workflows.&lt;/p&gt;

&lt;p&gt;This article will provide basic aliases that can be used as a&lt;br&gt;
starting point if you are not already using them. We will also&lt;br&gt;
discuss more complex aliases and hint at some possible custom commands to automate common workflows. Let's get started!&lt;/p&gt;
&lt;h1&gt;
  
  
  Simple Aliases
&lt;/h1&gt;

&lt;p&gt;An alias is a shorthand name for a command. Git recognizes&lt;br&gt;
the shorthand name and runs the full command. It may seem&lt;br&gt;
trivial, but they can be a significant time saver.&lt;/p&gt;

&lt;p&gt;The most frequently used commands in git are typically at&lt;br&gt;
least five letters long. People typically add two-letter&lt;br&gt;
aliases for each such as:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Alias&lt;/th&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ci&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;commit&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;br&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;branch&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;co&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;checkout&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rb&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;rebase&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sb&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;show-branch&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;st&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;status&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Aliases are most frequently defined globally in the user's&lt;br&gt;
configuration file found at &lt;code&gt;$HOME/.gitconfig&lt;/code&gt;. They can also be&lt;br&gt;
defined &lt;a href="https://git-scm.com/docs/git-config#FILES"&gt;system-wide and for a specific repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Try adding a global alias for the &lt;code&gt;status&lt;/code&gt; command by using the&lt;br&gt;
command below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git config --global alias.st status&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Test it out by running &lt;code&gt;git st&lt;/code&gt;. The status should be&lt;br&gt;
displayed. Simple abbreviation aliases are the most frequently&lt;br&gt;
used aliases. Aliases also support more complex configurations&lt;br&gt;
as well.&lt;/p&gt;
&lt;h1&gt;
  
  
  Complex Aliases
&lt;/h1&gt;

&lt;p&gt;Aliases support arguments or even running as shell&lt;br&gt;
functions. Using an alias in place of a frequently used&lt;br&gt;
command with arguments can make commands more intuitive and&lt;br&gt;
significantly speed up your workflow.&lt;/p&gt;

&lt;p&gt;Complex aliases are created in the same way as simple aliases&lt;br&gt;
but the arguments need to be quoted:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git config --global alias.staged "diff --staged"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Running &lt;code&gt;git staged&lt;/code&gt; now shows the diff of the last commit to&lt;br&gt;
the index. Here are some ideas for more complex aliases:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Alias&lt;/th&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;staged&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;diff --cached&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;unstage&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;reset HEAD --&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;whoops&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;commit --amend&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;redo&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;commit --amend --reuse-message=HEAD&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;last&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;log -1 HEAD&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Although Git supports running shell functions as aliases, it&lt;br&gt;
becomes challenging to accept arguments to these scripts&lt;br&gt;
without complex escaping. There is actually a simpler way to&lt;br&gt;
handle these more complex scenarios though – custom commands!&lt;/p&gt;
&lt;h1&gt;
  
  
  Custom Commands
&lt;/h1&gt;

&lt;p&gt;Git supports writing your own custom commands that can be&lt;br&gt;
called as you would use a regular git command. This is often&lt;br&gt;
the most maintainable way to add complex functionality. The&lt;br&gt;
logic can be contained in a completely separate script instead&lt;br&gt;
of inside your git configuration. Arguments to the git command&lt;br&gt;
are instead passed as arguments to the script which makes&lt;br&gt;
parsing or using them much simpler.&lt;/p&gt;

&lt;p&gt;To write a custom command, add an executable in your &lt;code&gt;$PATH&lt;/code&gt;&lt;br&gt;
prefixed with the name &lt;code&gt;git-&lt;/code&gt; so it can be discovered. For&lt;br&gt;
example, if you want a custom command called &lt;code&gt;custom-command&lt;/code&gt;,&lt;br&gt;
add an executable to your &lt;code&gt;$PATH&lt;/code&gt; named&lt;br&gt;
&lt;code&gt;git-custom-command&lt;/code&gt;. That's it! Git will find it and run it.&lt;/p&gt;

&lt;p&gt;This opens up the door for some really cool custom&lt;br&gt;
functionality. Many of the tasks that we perform each day can&lt;br&gt;
be automated. I use custom commands for everything in the&lt;br&gt;
table below:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;Alias&lt;/th&gt;
&lt;th&gt;Functionality&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;create-merge-request&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;cmr&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Create a GitLab merge request for the current branch and link it to its JIRA ticket.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;diff-branch&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;db&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Show the differences between the provided branches. Often called by other commands.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;find-branch&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;fb&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Do a search for a branch with the provided name. Run a &lt;code&gt;fetch&lt;/code&gt; if the name cannot be found then search again.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;find-branch-checkout&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;fco&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Same as above but then check the branch out.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;merge-diff&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;md&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Display the resulting diff from the provided merge commit.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;name-branch&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;nb&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Append a string to the local branch name to help distinguish it.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;remove-merged&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;rmm&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remove all local branches that have been merged to the primary branch such as &lt;code&gt;master&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;repo&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;Open the current repo in the browser at GitLab or GitHub.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;since-master&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;sd&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Show the diff of changes from the current branch compared to its merge-base with &lt;code&gt;master&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ticket&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;List the ticket name associated with the current branch. Often called by other commands.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;when-merged&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;wm&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Display the commit from when the provided commit was merged into the current branch.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;jira&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;Open the provided issues or current branch's ticket in JIRA.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You can see in the table above I included aliases for the&lt;br&gt;
custom commands. The names of these commands are often longer&lt;br&gt;
due to the more complex behavior. Fortunately, Git allows us&lt;br&gt;
to create aliases for custom commands just as we would for a&lt;br&gt;
builtin command!&lt;/p&gt;

&lt;p&gt;If you are interested in using any of these custom commands&lt;br&gt;
for your own use, you can find the source in my &lt;a href="https://github.com/briehman/dotfiles/tree/master/bin"&gt;local&lt;br&gt;
repository&lt;/a&gt;. These get installed to my home directory at&lt;br&gt;
&lt;code&gt;$HOME/bin&lt;/code&gt; which is included in my &lt;code&gt;$PATH&lt;/code&gt;. If you want to set&lt;br&gt;
this up for yourself locally, run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="nv"&gt;$HOME&lt;/span&gt;/bin/
  &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; ~/.bashrc &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"export PATH=&lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;HOME/bin:&lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;PATH"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/.bashrc
  &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; ~/.zshrc &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"export PATH=&lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;HOME/bin:&lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;PATH"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/.zshrc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can also see my complete &lt;a href="https://raw.githubusercontent.com/briehman/dotfiles/master/.gitconfig"&gt;Git configuration&lt;/a&gt; if you want to&lt;br&gt;
see what aliases I have configured. Finally, here are some&lt;br&gt;
other good guides on using Git aliases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases"&gt;https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://githowto.com/aliases"&gt;https://githowto.com/aliases&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://www.atlassian.com/git/tutorials/git-alias"&gt;https://www.atlassian.com/git/tutorials/git-alias&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, please share with me your favorite aliases or custom commands!&lt;/p&gt;

</description>
      <category>git</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
