Bedazzle Your Bash Prompt with Git Info

I have seen this around and this morning finally decided to try it out. Thus far I am finding it surprisingly helpful. If you put the following in your bash profile, it will show the current git branch in your terminal prompt.

bash profile addition

function parse_git_branch { ref=$(git-symbolic-ref HEAD 2> /dev/null) || return echo "("${ref#refs/heads/}")" } PS1="\w \$(parse_git_branch)\$ "

If you are not in a directory that is a git repository it will just provide a normal prompt but if you are in a directory with a git repo, you’ll get a prompt like the following, even when you switch branches:

~/dev/projects/httparty (master)$ gb integration * master ~/dev/projects/httparty (master)$ git co integration Switched to branch "integration" ~/dev/projects/httparty (integration)$

Pretty handy, eh? I actually bedazzled mine a bit more with color and the current time like this:

bedazzled bash profile addition

function parse_git_branch { ref=$(git-symbolic-ref HEAD 2> /dev/null) || return echo "("${ref#refs/heads/}")" } RED="\[\033[0;31m\]" YELLOW="\[\033[0;33m\]" GREEN="\[\033[0;32m\]" PS1="$RED\$(date +%H:%M) \w$YELLOW \$(parse_git_branch)$GREEN\$ "

Here is a screenshot of my prompt.

Hawt. Light red blends into the black so as not to stand out. The yellow does standout, but that is because the current branch is more important to me than the directory I am in. The green color and black background is from the Homebrew theme in Terminal (requires Leopard). The reason I added the time is that I do not include the time in my menu bar. If you don’t like the colors I chose, you can pick from a list.

Try this out if you haven’t yet, I guarantee you will dig it. For those of you out there that use a different shell (zsh, etc.), feel free to post how to do the same in the comments.