Bash Autocompletion for Git
The default installation of Git on OS X doesn’t include support for autocompletion. I know it’s possible, because whatever package I used to install Git on my Windows work computer has it, but I’ve never taken the time to figure it out. Well, here’s how it works. Note that these instructions were about 10x longer, until I decided to read the contents of the bash-completion.bash script… the installation is a lot easier than I was making it.
First, you need to copy the bash-completion script somewhere appropriate.
cd /usr/local/git/contrib/completion/ cp git-completion.bash ~/.git-completion.sh
Now add the following line to your ~/.bash_profile
source ~/.git-completion.sh
Next time you login, autocompletion in Git should work just fine. If you want to take it one step further, here’s how you add the current branch to your command line. The bash-completion script tells you to change your PS1 to the following.
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
Which is cool, but I already had my PS1 set up, complete with colors and shit. Here’s what I ended up doing.
GIT_PS1_SHOWDIRTYSTATE=1 GIT_PS1_SHOWSTASHSTATE=1 GIT_PS1_SHOWUNTRACKEDFILES=1 export GIT_PS1_SHOWDIRTYSTATE export GIT_PS1_SHOWSTASHSTATE export GIT_PS1_SHOWUNTRACKEDFILESPS1='[\e[1;32m]$(__git_ps1 " (%s)")[\e[m]' PS1="[\e[1;36m]\h:\W[\e[m]$PS1[\e[1;36m]> [\e[m]" export PS1
What a mess! I’m never going to be able to remember what I did here unless I write it down here.
First off, the GIT_PS1_ variables add an indicator to your command line letting you know that you’re in a dirty working directory, you have something stashed or there are untracked files. Handy.
Second, anything between [\e[1;32m] and [\e[m] is given a color. You can read more about it here.
Finally, the function $(__git_ps1 " (%s)") will replace %s with the current branch (plus a flag if you’ve set those variables I described earlier) if you are currently in a directory under Git source control.
The final result:

I’ll be updated my recent post on my .bash_profile to reflect these changes.