Lately I’ve been using some longer, and not very memorable, git branch names. I love bash completion and was hoping I could add items to this auto-completion dynamically on request. It turns out this is really simple to do. Thanks to this fab tutorial, a quick code snippet like this in my .profile and I’m all set.


_complete_git() {
  if [ -d .git ]; then
    branches=`git branch -a | cut -c 3-`
    tags=`git tag`
    cur="${COMP_WORDS[COMP_CWORD]}"
    COMPREPLY=( $(compgen -W "${branches} ${tags}" -- ${cur}) )
  fi
}
complete -F _complete_git git checkout

Now anytime I’m in a path with a .git directory, I can just use ‘git checkout [tab]‘ and I’ll git tag and branch auto-completion goodness.