How to always display your active git branch in the bash prompt

When work­ing on git, it can some­times get annoy­ing to have to run git sta­tus to check which branch you are cur­rently on. You can avoid your­self those extra key­strokes and be a bit more pro­duc­tive if you can set your shell prompt to always show you the active branch name.

You can do the following.


Cre­ate a file any­where in your file sys­tem and add the fol­low­ing lines to it. For sim­plic­ity sake, I will cre­ate the file ~/bash_prompt_git_branch.

parse_git_branch() {

    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'


export PS1="\[email protected]\h \W\[33[32m\]\$(parse_git_branch)\[33[00m\] $ "

Then, sim­ply open your ~/.bashrc file and add this to the end of the file

$ source ~/bash_prompt_git_branch

Open a new ter­mi­nal and tra­verse to a new direc­tory to view the changes, or, just do source ~/bash_prompt_git_branch for the changes to reflect in the cur­rent terminal.


Cre­ate a new file called ~/.bash_profile if it not already exists

touch ~/.bash_profile

Add the lines

parse_git_branch() {

    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'


export PS1="\[email protected]\h \W\[33[32m\]\$(parse_git_branch)\[33[00m\] $ "

Then run the fol­low­ing com­mand in your ter­mi­nal ses­sion to enable the setting.

$ source ~/.bash_profile

There’s prob­a­bly a way to make this set­ting more per­sis­tent on a Mac. If you know what it is, let me know in a com­ment and I will update the post.

Ryan Sukale

Ryan is just a regular guy next door trying to manage his life and finances.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *