Git aliases can help you save time by saving your favorite commands (and complicated options) into one terse command. These aliases can be simple as a single letter to stand in place of a complicated set of options. You will learn more about aliases below, including what they are, how they work, example aliases, and where to put them in your Git configuration.
Simple Commands and Complex Options
One of the things you will notice when using Git is that there are so many commands but perhaps only small handful that you will use regularly. And those commands you use regularly come with a variety of complicated options that expand that command. The git log
command, for example, gives you a complete log of your project, but you have over a hundred different options and customizable arguments for this one simple command. While it’s not too difficult to remember git log
it might be more difficult to remember git log --graph --decorate --oneline --all
. And, even if you could remember all of those options you probably would not want to type the whole thing every time. This is a perfect instance in which a simple alias can be very helpful.
How To Create Git Aliases
The easiest way to create custom Git aliases for your workflow is to add them to your Git configuration file. Below, you will see an example of a very simple Git configuration file. This is what you would use as a local level configuration. These values will not interfere with configuration in an individual project (.git/config
).
[user] name = Joe Example email = [email protected] [alias] st = status logg = log --graph --decorate --oneline --all cm = commit df = diff dfs = diff --staged [core] editor = emacs
Example Alias For Detailed Log
Notice all of the aliases in the configuration above. Instead of typing git status
, the first alias in this list allows you to simply type git st
. Maybe that’s not the biggest time saver, but take a look at logg
, this extended variation of git log
will give you all the extra options to produce a more minimalist, descriptive, and colorful log output.
So instead of typing this:
git log --graph --decorate --oneline --all
You can simply type:
git logg
In this example, the shortcut alias provided reads “logg”, with the extra “g” meant to distinguish the alias from the actual git log
command. This gives you both options, but you can name your alias anything you want. You could even name it “log” to outright replace the default log
behavior.
“Git Add Alias” On The Fly
You can also add aliases without having to create entries in your configuration file. You can create aliases on the fly, and they will be added to the system wide list of aliases following this format:
git config --global alias.<alias> <full command>
Here are some examples put forth in the Git documentation:
$ git config --global alias.co checkout $ git config --global alias.br branch $ git config --global alias.ci commit $ git config --global alias.st status
So that’s how you can use Git aliases in your configuration to speed up your Git workflow. What’s the next step on your Git journey?
Is this useful if I wanted to replicate the old Macros used in Basic programming. Where we could string together a set of commands and use a single key to execute all commands. Need something like this yesterday. Thank you, Rob
Hi, Rob! Aliases are similar to macros, but they’re really only relevant for Git commands in this context. For writing more general aliases, you might want to add them to your Bash configuration. For more complicated procedures, writing a Bash script will probably be most efficient. If you’re programming, you should check and see what macro features are available in your text editor. I like to mess around with Visual Studio Code, which has a lot of hotkey features built in, and I know the author of this article is a big fan of Emacs which has a ton of macro-based tools available. Hope that helps!