Last Updated: September 04, 2018
· plato2013

Show current Git branch name in bash PS1

Git comes with a handy shell script that could be used to display the actual branch name within the bash. This could protect you from doing the right stuff with the wrong branch.

On Linux

If you've installed git via a package manager the script itself is installed to "/etc/bashcompletion.d/git" and all you have to do is to add the following lines either to your local bashprofile or the global version of your system:

# Show current Git branch on bash prompt
source /etc/bash_completion.d/git
PS1="[\[\033[32m\]\w]\[\033[0m\]\$(__git_ps1)\n\[\033[1;36m\]\u\[\033[32m\]$ \[\033[0m\]"

After a relogon your PS1 should be extended as soon as you are in directory which is under version control by git. This will look similar to this:

[/home/hero/best_project] (master)

On Mac OSX

If you have installed the Git version that is provided by Apple you can add the following to your ~/.bash_profile:

source /Library/Developer/CommandLineTools/usr/share/git-core/
PS1="[\[\033[32m\]\w]\[\033[0m\]\$(__git_ps1)\n\[\033[1;36m\]\u\[\033[32m\]$ \[\

2 Responses
Add your response

I ended up using your instructions but with an inline PS1 Prompt based on Linux Mint's default:

PS1="\[\e[1;32m\]\u@\h\[\e[1;34m\] \w\[\e[0m\]\$(__git_ps1)\[\e[1;34m\] \$\[\e[0m\] "
over 1 year ago ·

I think you meant to color the left bracket as well...

source /Library/Developer/CommandLineTools/usr/share/git-core/ PS1="[\033[32m][\w][\033[0m]\$(_gitps1)\n[\033[1;36m]\u[\033[32m]$ [\033[0m]" </code>

Thanks for your help!

over 1 year ago ·