Git on Board with Saving Keystrokes
How to create a git alias to save time and increase efficiency
There’s always something new to learn about git. In this tutorial I want to share a great git feature with you. But this feature is not another crazy and complex command that you have to memorize. Instead, this is a feature that lets you memorize less. Intrigued? Read on.
There’s one thing about the daily use of git that can get quite cumbersome: all the repetitive typing.
git commit -m “message”,
git checkout master,
git checkout branch,
git add -A,
commit -m “message”... That's 149 keystrokes. There’s gotta be a better way!
And there is.
Let’s give a warm welcome to Git Aliases.
What is a Git Alias?
A Git Alias is as sounds: a shortcut for a longer command. The basic use for setting up an alias is to shorten common
commands that you type everytime you use git. For example, with a git alias you only need to type
git co instead of
git checkout or
st instead of
git status. But the magic really happens when you start using aliases for more complex, multi-step commands.
By the way, there’s also another concept called bash aliases. That’s similar, but different. This tutorial is about git aliases and assumes you are already familiar with git and the command line. If you are and you’re ready to streamline your git flow, then let’s dive on in!
How to Create a Git Alias
Since you’re already using git, then you have a configuration file called
.gitconfig that lives in your $HOME directory.
Out of the box, it has some basic user info such as your name and email address, and we’re going to add more to it today.
git status as our first example. To set an alias for
git status, you simply type this command into your terminal
$ git config --global alias.st status
--global flag sets the alias so that it will be available in any git repository on your machine.
In this case, the
st represents whatever you want the shortcut to be, and the last word is the command it is taking the place of. (If the
command that is being shortened is longer than one word, it needs to be in quotes).
Now when you are in the terminal and want to check git status, you can just type
These are a few more basic ones that you might find useful
$ git config --global alias.co checkout $ git config --global alias.cob 'checkout -b' $ git config --global alias.com 'checkout master' $ git config --global alias.ci commit
Let’s Step It Up
One set of commands that I find most tedious is this:
$ git add -A $ git commit -m "message"
So let’s set an alias that combines both of these commands into one.
$ git config --global alias.ac '!git add -A && git commit -m'
The ! is necessary when stringing together multiple commands. Now when you want to commit all your files (including untracked files) and make a commit, you can type
$ git ac "Your commit message"
That’s so much shorter, right?!
One More Example
We've seen that git aliases can be used to simplify commands. Another use case is to rename commands. For example,
reset HEAD -- is used to remove a file from the staging area. It’s not necessarily a long command, but it’s not
an intuitive name either. Since we are unstaging files, why not just call it "unstage"?
$ git config --global alias.unstage 'reset HEAD --'
Now you can simply type
$ git unstage /file/path.html
$ git reset HEAD -- /file/path.html
That’s an improvement in my book!
Some Final Tips
Thank you for taking the time to read this tutorial. I hope it has helped you to find a more efficient git
workflow. You have learned about your
.gitconfig file, how to set up basic git aliases, and have seen an
example of a slightly more complex alias that combines multiple commands into one. Before we go, I’ll
leave you with a couple final alias tips.
First of all, only create an alias for what you commonly use and for what will actually be helpful. There are a number of git alias lists out there, but there’s no need to add them all. It just doesn’t make sense to memorize a multitude of aliases, especially ones that are not used on a regular basis. Start with a few obvious basics, get used to using those, and then only create others as you realize they would be useful to have.
Lastly, now that you have added your aliases, how can you review what you have in place? You have a few options. In the command line, you can enter
open ~/.gitconfig- This will open the file in your default text editor.
code ~/.gitconfig- This will open the file in VS Code if that’s what you use and you’re set up the code command.
git config --list --show-origin- This will show the contents of your config file. This will also show the path to your config file if you’re not sure where it is.
Any text editor can be used to view your file. Once open in a text editor, you can also manually add an alias within the file instead
git config --global alias.shortcut command in the command line.
Here's the alias section of my config file with all the aliases discussed in this tutorial.
Remember the 149 keystrokes at the beginning of this tutorial? With these aliases, those 149 keystrokes turns into only 81 keystrokes. Extrapolate that over all the coding you do in a day and that's some serious time savings!
And that’s it! Congratulations on learning something new about git!