DEV Community

Cover image for Save Time using Git Configurations
Marc Philippe Beaujean
Marc Philippe Beaujean

Posted on • Updated on • Originally published at byteschool.io

Save Time using Git Configurations

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.

User Configurations

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:

https://USERNAME:PASSWORD@github.com

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:

git config --global credential.helper 'store --file ~/.git-credentials'

Git Aliases

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:

git config --global alias.shortcut 'command-we-want-our-shortcut-to-represent'

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:


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'

Bash aliases to automate repetitive sequences

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:

#!/bin/sh
git add .
git commit -m "$1"
branch_name=$(git branch | grep \* | cut -d ' ' -f2)
git push origin $branch_name

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:

alias gacp='~/my_folder_for_bashscripts/git_add_and_commit_and_push.sh'

Give your bashscript permissions to run:

 chomd +x ~/my_folder_for_bashscripts/git_add_and_commit_and_push.sh

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.

Top comments (0)