How to always display your active git branch in the bash prompt
Ubuntu
Create a file anywhere in your file system and add the following lines to it. For simplicity sake, I will create the file ~/bash_prompt_git_branch
.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \W\[33[32m\]\$(parse_git_branch)\[33[00m\] $ "
$ source ~/bash_prompt_git_branch
source ~/bash_prompt_git_branch
for the changes to reflect in the current terminal.
Mac/OSX
Create a new file called ~/.bash_profile if it not already exists
touch ~/.bash_profile
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \W\[33[32m\]\$(parse_git_branch)\[33[00m\] $ "
$ source ~/.bash_profile