Save Time using Git Configurations
Tags: git, productivity, beginners, bash
Published: 2019-11-13 20:57:19

<p>There are many things about the git workflow that can be automated - here are some of my favorite configurations and methods to make my day-to-day life a bit simpler when using git. Only prerequisites for this tutorial is a basic understanding of how git works - everything else will be explained on the fly.</p>
<h2>User Configurations</h2>
<p>One of the first steps would be to remove the requirements of having to enter login details whenever you are trying to push to a remote repository. This is only a problem on unix based operating systems - from my experience, git does this by default on windows. To store our credentials, we want to create a random text file. I will be using the file path ~/.git-credentials. Add a line with the following pattern to your new file:</p>
<pre><code class="language-bash">https://USERNAME:PASSWORD@github.com</code></pre>
<p>If you are not using github, you will want to change the url after the @ sign with your respective git provider. Now, enter the following command so that git stores your credentials for all future HTTPS requests:</p>
<pre><code class="language-bash">git config --global credential.helper 'store --file ~/.git-credentials'</code></pre>
<h2>Git Aliases</h2>
<p>Git has included a feature that allows us to abbreviate some of the more wordy features in its arsenal: aliases. A git alias (similar to aliases in regular bash scripting - more to this later) allows us to define a custom shortcut for a git command. The basic structure for a git alias goes as follows:</p>
<code class="language-bash">git config --global alias.shortcut 'command-we-want-our-shortcut-to-represent'</code>
<p>In other words, after alias., we can put our shortcut that we want to define, followed by the git command it is supposed to represent. Now, typing "git shortcut" will execute the command that I defined. Here are examples of commands recommended by the official git site that I have adopted:</p>
<pre><code class="language-bash">
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.co checkout
# feature only available in newer versions of git
git config --global alias.cb 'branch --show-current'
</code></pre>
<h2>Bash aliases to automate repetitive sequences</h2>
<p>I often find myself repeating certain sequences when using git. The most common workflow is the classic add, commit, push. Using a bash script (sorry windows users!), we can automate this tedious routine into a single command (and hopefully give you an idea of how you can automate further parts of your specific workflow). Create the following bash script with an editor of your choice:</p>
<pre><code class="language-bash">#!/bin/sh
git add .
git commit -m "$1"
branch_name=$(git branch | grep \* | cut -d ' ' -f2)
git push origin $branch_name</code></pre>
<p>I want to keep it short and simple in this tutorial, so I won't be explaining the code in too much details - this is not a bash scripting tutorial after all. What it does is execute the typical git commands you would if you wanted to push the current state of your application to the remote origin. The commit name is passed to the script as a variable (that is what the $1 means). Also of note is that it automatically pushes to the branch that the user is currently on, meaning you don't have to worry about your branch being the current upstream (something that frustrated me a lot, but others claim is necessary to prevent unintended pushes to master). To declare this script as a command for our bash terminal, we want to edit our .bashrc file, which is available at the directory path ~/.bashrc in unix. At the bottom of said script, add the following line:</p>
<pre><code class="language-bash">alias gacp='~/my_folder_for_bashscripts/git_add_and_commit_and_push.sh'</code></pre>
<p>Give your bashscript permissions to run:</p>
<pre><code class="language-bash"> chomd +x ~/my_folder_for_bashscripts/git_add_and_commit_and_push.sh</code></pre>
<p>Now, open a new terminal in a directory with a git repository and type "gacp", followed by a desired commit message! I hope this saves you lots of time - if you are already an experienced developer, I would love to hear some of your own git tricks that make your life a bit easier.</p>