This is the second in a series of posts designed to help you supercharge your workflow.
In my last post I mentioned aliases in passing. A few readers asked me for more information, so I thought I’d write another post.
An alias is essentially a shortcut, or abbreviation, which lets you “remap” any command to run one or more other commands. Many developer / command-line tools support aliases, from command-line shells like bash, zsh, or PowerShell, to version-control systems like Git or Mercurial. I’ll give concrete examples for bash and Git in this post.
Before I do, though, I want to talk about the reasons one might create an alias. I find that the aliases I need can be classified into a few categories:
These aliases are all about speed, so they’re as short as possible.
git diff takes too long to type? Alias it to
The most common example of this is the
ll alias for
ls -l, now included
in many Linux distros by default.
I also include in this category aliases that cover common typos, for example:
cd ..(leaving out the space)
rspec(transposing the s and the p)
git(off-by-one error on the other hand)
I started working as a software developer before Linus created Git, in what
many consider “the dark ages”. As such, I spent a few years using RCS and CVS
for version control. In RCS the commands for
check out and
check in are
ci; these commands were preserved in CVS as aliases for
As Git became popular and our projects switched over to using it exclusively, a senior developer I worked with set up extensive aliases so he could continue using the same commands he’d been used to. I try not to go that far, but muscle memory is powerful — and aliases like this can save you a lot of pain.
Frame of mind
This one actually started as a backwards compatibility alias, but its role has
really evolved for me over time. Most version-control systems have a command
that shows you which user last modified each line in a file. In SVN, this
commmand is called
blame, but it has aliases:
When we switched to Git, that same senior dev installed
praise as an alias for
blame. Even though I’ve never used SVN heavily, I still invoke
time, because it reminds me to be charitable with my fellow developers.
In Bash, aliases are created using
alias, and destroyed with
1 2 3 4 5
Personal aliases are typically defined in the
Note that aliases can cover or “shadow” existing commands. This can be both
frustrating and awesome. For example, if you alias
ls -G, all your
ls will hit the alias, not the base command.
tldp.org has more information on aliases in Bash.
There are two ways to create Git aliases. The first is via command line:
The second way is via editing a
~/.gitconfig (global) or
.git/config (per-repository) file (this is my actual gitconfig file):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
You can shell out in a Git alias, by starting the alias with a
It’s worth noting that the awesome git-completion supports tab completion for aliases, as well as Git builtin commands. You should probably install it, if you haven’t already :)
For more information on Git aliases, see the Pro Git book (pay for it!), or check here.
Here are a few samples of real-world Git aliases. Check these out, steal them for your own use, and remix them to increase your productivity!