There are two main use-cases for creating Git shortcuts:
- The command is used very often, and the shortcut will save a keystrokes every type:
git commit --amend --no-edit
- The command is used very seldom, and it’s hard to remember the exact correct options:
git log --graph \ --abbrev-commit \ --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'
In both cases, there are different ways to create shortcuts over Git commands.
The easiest and most straightforward way is to use standard shell aliases:
alias gitamend='git commit --amend --no-edit'
|This can get pretty ugly if options themselves contains simple quotes, as in the above |
The benefit of the shell alias way is that it can do everything a shell can, e.g. piping commands, executing sub-shells, etc.
For example, the following is a shortcut I use: it checks out the next commit in the commit history - quite useful for demos to jump across commits:
alias gitnext='git checkout $(git rev-list --topo-order HEAD..master | tail -1)'
The previous method is by no way Git specific. Yet, Git also offers an aliasing feature by itself, through the
git config command. The syntax is quite straightforward:
git config alias.<shortcut> <command>
For example, I use this approach for the following shortcut:
git config alias.amend 'commit --amend --no-edit'
It’s now possible to simply call:
This way only configures the shortcut in the current Git repository. To be able to call it regardless of the repository, use the usual
git config --global alias.amend 'commit --amend --no-edit'
It’s also possible to call external binaries, by prefixing it with
git config alias.sourcetree '!/Applications/SourceTree.app/Contents/MacOS/Sourcetree'
The last method is the most powerful, but also the most brittle.
|I didn’t even found where it’s referenced in the Git documentation, or even if it’s referenced at all. Pointers welcome.|
Just create any executable or script on the
$PATH, and name it
#!/bin/bash echo Hello $1;
At that point, the previous script can be called like this:
git foo John
There are several ways to create shortcuts in Git: plain bash aliases, proper Git aliases, and aptly-named executables.
- The first way is not Git specific
- The second one is able to handle Git command and options combinations
- Last but not least, the third way allows to integrate any executable into the Git flow