Shell commands as git aliases


To save typing, I have a couple of aliases set up in git. For example, to get the last hash, I simply type git hash. I did this by setting up the following alias:

$ git config --global alias.hash 'git log -1 --pretty=%h'

Now, I found out that basically anything can be aliased in this way. So one can create git commands with any shell command. The only thing is, if you use a non-git command, prefix it with !. This is how you’d do it in vim. Silly example, to demonstrate:

$ git config --global alias.foo '!echo foo'
$ git foo
foo

This is very useful if you need to pipe the output of a git command to a shell filter like sed, awk, grep… Here a more useful example:

$ git config --global alias.t '!git show-ref --abbrev=7 --tags | sed \"s|refs/tags/||\"'
$ git tag mytag 4d3h779
$ git t
4d3h779 mytag