Current Git Branch in Bash Prompt

Good trick; saves typing git status or git branch so often. Note: it’s not very clear from the article, but the code for the prompt goes in ~/.bashrc.


I wasn’t interested in colors, and I’m fairly happy with Terminal’s standard prompt (\h:\w \u\$), so I ended up with something pretty close to the default:

parse_git_branch() {
  git name-rev HEAD 2> /dev/null | sed 's#HEAD\ \(.*\)#[\1]#'
}
export PS1="\h:\w \u\$(parse_git_branch)\$ "

Which yields a prompt like Pangu:~/programming/git/ratafiacurrant stilist[master]$.

The downsides are that it can get rather long (especially since I keep my Terminal window width to 80 characters plus scrollbar) and its placement can seem a bit odd.

written 20 January, 02010 Comments