Making a git alias alias
The git version control system supports an alias feature that provides a way to add shorthand names for frequently used commands. I’ve set up several aliases over time for frequently used or interesting commands. For instance, a ‘logfiles’ alias that lists a log of commits with the paths to the files that were part of that commit. Where a simple ‘git log’ would list the commit hash, author, date and comment:
$ git log commit 4d95176452dce3ee57065508b4eace98810a2859 Author: Curious One <curious1@curiousprog.com> Date: Wed Aug 19 06:56:30 2015 -0400 Move all spa-related files to a separate spa subdirectory |
…the ‘git logfiles’ alias includes a list of the files in the commit (via the ‘–name-only’ option to ‘git log’):
$ git logfiles commit 4d95176452dce3ee57065508b4eace98810a2859 Author: Curious One <curious1@curiousprog.com> Date: Wed Aug 19 06:56:30 2015 -0400 Move all spa-related files to a separate spa subdirectory spa/angular-spa-bootstrap-tabs.html spa/angular-spa-controllers-spec.js spa/angular-spa-controllers.js spa/angular-spa.html spa/angular-spa2.html spa/spa-pages/about.html spa/spa-pages/contact.html spa/spa-pages/home.html spa/spa-pages/tabpanel.html |
After time, with a bunch of aliases added, I tend to lose track of some of them. So I created a git alias named ‘alias’ to list them.
$ git config --global alias.alias '!git config --list | grep alias | sort' |
The command starts with an exclamation point which means that it runs in the context of a command line shell. It fires off ‘git config –list’ to list all configuration variables, then pipes to grep to filter out just the config variables in the ‘alias’ config section, and finally sorts the results.
So with the “alias” alias defined, there is a simple, memorable command to list all of the aliases defined for git in my login account:
$ git alias alias.alias=!git config --list | grep alias | sort alias.branchinfo=show-branch --sha1-name --list --more=12 alias.graphic-log=log --graph --oneline --decorate alias.logbrief=log --pretty=oneline --abbrev-commit alias.logfiles=log --name-only |
TL; DR
Creating a git alias that lists all aliases defined in the user’s login environment:
$ git config --global alias.alias '!git config --list | grep alias | sort' |
References
“Pro Git” by Scott Chacon and Ben Straub, Apress – an online-readable version of the book
“Git Magic” by Ben Lynn, 2010
Versions
$ git –version
git version 2.10.1 (Apple Git-78)