Git auto-completion in bash

This was another one of those tips that I wish I had found long, long ago.  It gets tiring typing branch names over and over again, but thanks to git's bash auto-completion script I no longer have to.

The bash completion script is stored at /etc/bash_completion.d in Ubuntu, other distributions might store it in a different location.  Add the following lines to your ~/.bashrc file:

# next line only necessary if not included in /etc/bash_completion.d/
source /etc/bash_completion.d/git-prompt

GIT_PS1_SHOWCOLORHINTS=true
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWUPSTREAM="auto"
PROMPT_COMMAND=' __git_ps1 "\[\e[32m\]\u@\h\[\e[00m\]:\[\e[34m\]\w\[\e[00m\]" "\\\$ "'

This will show you the current branch you are in, the staged (+) or unstaged (*) states, as well as whether or not you have stashed ($) and untracked (%) files in your prompt.

jdoe@host:dir (feature/www-123-branch *$%)$

It will also give you auto-completion for branches, tags, subcommands and more.