Git branch in your bash prompt

Git is great, I love it. Currently, I'm really into creating branches for stuff I have to do, without bothering the rest of the repo. Because of this, I'm constantly switching branches. Sometimes I'm lost and changing stuff in the wrong branch. Of course, Git is wonderful in resolving these issues (reverting, cherry-picking, etc), but because knowing which branch I'm using probably works better (and saves time), I added the following one-liner to my Bash prompt:

$(git branch &>/dev/null; if [ $? -eq 0 ]; then echo " ($(git branch | grep '^*' |sed s/\*\ //))"; fi)

when entering a Git repo subdir results in:

wouter@mini-me:~/prjs/git/myproject (query_builder)$

Otherwise just displays:

wouter@mini-me:~/$

For completeness, my complete PS1 (bash on ubuntu) looks like:

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(git branch &>/dev/null; if [ $? -eq 0 ]; then echo " ($(git branch | grep '^*' |sed s/\*\ //))"; fi)\$ '

0 Responses to “Git branch in your bash prompt”


  1. No Comments

Leave a Reply