Show current git repo in command prompt

Dan showed me at a snippet to slap into your ~/.bash_profile to display the current git branch in your prompt for the working directory (provided it’s a git repo). Very cool stuff.

Here is my version with colours changes to suit my tastes:

function parse_git_branch_and_add_brackets {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\ [\1\]/'
export PS1="\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[36m\]\[\033[0;33m\]\$(parse_git_branch_and_add_brackets)\[\033[00m\]$ "

It looks a little something like this:

Git Branch on Command Line

Mike Walker

Mike is a full-stack web developer with 15 years experience. I focus on PHP & Javascript development, and work with platforms like Shopify Plus and WooCommerce eCommerce. The Agency Developer is a weekly curated newsletter with a mission to inform and educate developers working with digital agencies.

Leave a Reply

Close Menu