Add autocomplete to git

In bash in Mac OS X, you can use [TAB] to autocomplete file paths. Wouldn’t if be nice if you could do the same with git commands and branch names?

You can. Here’s how.

First get the git-completion.bash script (view it here) and put it in your home directory:

curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-
completion.bash -o ~/.git-completion.bash

Next, add the following lines to your .bash_profile. This tells bash to execute the git autocomplete script if it exists.

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

Now open a new shell, cd into a git repo, and start typing a git command. You should find that [TAB] now autocompletes git commands and git branch names.