Git bash-completion with aliases?

I have created quite a few aliases for git commands, as I expect most everyone has. This works great except for one thing: git’s bash-completion script to fill in the branch you want to work on doesn’t work for the aliases. The version I have (see https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash) states that functions used to define aliases can participate in auto-completion through the following configuration:

# If you use complex aliases of form '!f() { ... }; f', you can use the null
# command ':' as the first command in the function body to declare the desired
# completion style.  For example '!f() { : git commit ; ... }; f' will
# tell the completion to use commit completion.  This also works with aliases
# of form "!sh -c '...'".  For example, "!sh -c ': git commit ; ... '".

However, I cannot get that to work – I’m obviously missing something here.

First, I’ve got the git-completion script loaded in my .bashrc:

### git autocomplete
if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi

Next, I’ve got this function defined:

function gco() { 
  : git checkout 
  git co $1 
}

Quite frankly, I don’t know what they mean by the “!f()…” comment, but since this was written so many bash-years ago, I’m guessing that it’s outmoded syntax… I’d appreciate any clarification on this; googling has not been my friend.

Bottom line, it doesn’t work. If I type:

gco [tab]

I get bash completion; and if I type:

git co [tab]

I get git completion.

Any suggestion as to what I’m missing?

Update
I still don’t know why the “null command” (:slight_smile: option doesn’t work. But I did find another way to resolve this. Part of the git-completion script includes the function __git_complete which can be used to explicitly alias the alias to the correct git command like so:

__git_complete gco _git_checkout

Hats off to stackoverflow and the first answer to this question.

1 Like