(Yes the title of this post is a pun, apologies!) I have written about aliases before! See for example a collection of my favorite git aliases or peruse my personal list on Bitbucket. Recently at our Summit I showed a simple technique that can really unleash the power of your Git command line. Several people commented that it was very useful and I am happy to elaborate on it some more here. Let me know your genius creations in the comments! (If you’re an old time git wrangler you know this already but it already but if not, read on!) Simple aliases are simple: you just add a line in the [alias] section of .gitconfig with your shortcut. For example: 1ls = log --oneline Will allow you to type git ls to get a bare list of short commit ids and commit messages. Or one of my favorites: 1caa = commit -a --amend -C HEAD Which will take all uncommitted and un-staged changes currently in the working directory and add them to the previous commit, amending it before pushing the change up. I use this all the time. Limitations of simple aliases There are many simple and useful aliases that you can craft like the above, but once you start to streamline your day to day routine you quickly realize there are some limitations to what you can do with simple aliases: Normal aliases can’t have parameters. You can’t execute multiple git commands in a single alias. You can’t use | (pipes) or grep. Or can you?! Actually there is a solution! Advanced alias template git allows you to shell out in aliases – that is to escape to a shell (like bash or zsh) using the ! (bang). This opens a new world of possibilities for your aliases. By forking a shell process we are able to access a whole lot of things: Shell expansions and parameters Use multiple git commands Pipes, greps and all Unix command line tools as needed BEAUTY! Here is the template for an advanced alias that requires parameters: 1my_alias = "!f() { 〈your complex command〉 }; f" The trick is to wrap our git command in an “anonymous” bash function – or better said a function named “f“. Doing it like this we have access to command line variables and shell expansions as the following: $1 to mean the first parameter passed to the command. $2 to mean the second parameter passed to the command. (and so on for $3, $4, etc.) $@ to mean all command line parameters passed. We can also now chain git commands with && and use the entire Unix toolkit. Example advanced aliases Now that I whet your appetite, let’s see a few examples of what you can do with all this power. Here a few ideas: Quickly add a remote pointing to your colleagues forks 123ra = "!f() { \ git remote add $1 https://bitbucket.org/$2.git; \ }; f" Define the above […]