MacOS – git auto-complete branches with checkout alias

auto-completebashcommand linegitmacos

There's this neat script to autocomplete git branches.

It works fine with git checkout <tab>.

But, I want to take it further. I have a checkout alias: alias co='git co'.

How can I make branch autocompletion work using co <branch>

Best Answer

I have this in my .bashrc which works okay:

alias gco="git checkout"
__git_complete gco _git_checkout

If you open up that neat script you'll see method names like __git_checkout. You can get completion for each of those methods using this method.

Unfortunately, commands like git pull don't play nice when you try to tab complete the branch name (the second argument), for example given this alias:

alias gpl="git pull"
__git_complete gpl _git_pull

This behavior occurs:

gpl origin master
      ^      ^
 tab ok      doesn't tab