git bash completion + git aliases

If you haven’t added bash completion for git you should do so now. As with all bash completion it just makes you feel happy :). I’ll wait while you do that….

Now, if you’re like me you have tons of git aliases setup. Shortly after setting up bash completion you will notice that your aliases don’t work with the new found goodness. What is a poor developer to do? Can’t I have bash completion with my aliases thus reducing the actual typing I have to do to close to zero? Well, not knowing too much about how bash completion works this was the best solution I came up with:


## in my .bash_profile

#Git aliases
alias gco='git co'
.....

source ~/bin/git-completion.bash
complete -o default -o nospace -F _git_checkout gco

If you look in git-completion.bash at the bottom you will see where it declares which bash functions are suppose to handle which git commands (via the built-in ‘complete’ command.) What I did was just find the correct function and assigned it to my corresponding git alias after the git bash completion file was sourced. I can now type ‘gco ‘, hit tab, and see all of my branches. Pretty cool, huh? I don’t think I can get any lazier.

I know that my bash-fu is still weak and I have much to learn… Is there a simpler way to do this so that bash completion expands your aliases? If not, I hope this helps other lazy git users.


About this entry