Add Git Branch Name to Terminal Prompt (Mac)

When in a repository directory you can show the name of the currently checked out Git branch in the prompt, making it easier to track where you are (and where you’re about to commit).


The following instructions are written for Mac OS X (Lion) or later, using a terminal with bash. However, you can use more or less the same approach on Linux, just with a different configuration file.

Your data problems, solved

On-call analysis, machine learning and visualisation

Get useful information from your data, quickly and accurately with on-call data science consultation and analysis.

Together we'll build evidence-based solutions to your technical and business challenges.

Rapid solutions to your data problems, one-to-one consult and on-call analysis.

Available for consultation 8am-6pm CET/CEST Online

Ready to build your own apps?

Create Simple GUI Applications with Python and Qt

The complete beginners guide to building cross-platform GUI applications with Python. Step by step from displaying your first window, to fully functional and useable software.

Join 300+ readers already using this book to build awesome things.

77 page ebook, 2.5 hours of video tutorials

Learning Python?

Join the Academy for access to free tutorials, courses and books

Expand your Python knowledge, from absolute basics through application, development, web and data science. New resources added regularly.

Boost your coding skills with up to date Python resources.

Access 100s of tutorials, courses, videos and ebooks


Open the Terminal app and if the file ~/.bash_profile does not already exist create it with the following command.

touch ~/.bash_profile

Harry Cutts points out in the comments that you should be able to use the file ~/.bashrc instead to make this method applicable to Linux.

Open ~/.bash_profile in your favorite editor and add the following content to the bottom.

# Git branch in prompt.

parse_git_branch() {

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


export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "


  • Depending on configuration changes you may have made previously you may already have a PS1 variable being exported. In this case you will need to add \$(parse_git_branch) somewhere in the existing value.

  • The \[\033[32m\] parts set the color of the branch text. If you prefer another color check online for a reference on valid values.

  • The \ in \$(parse_git_branch) is important to ensure the function is called each time the prompt is displayed; without it, the displayed branch name would not be updated when, for example, checking out a different branch or moving in and out of a Git repository directory.

Now when you change to a directory that is within a Git repository, the prompt will be supplemented with the name of the current branch. When you switch branches the prompt will update accordingly.

If you are using an existing Terminal session, don’t forget to make the changes take effect by sourcing the file with the command source ~/.bash_profile.

Get regular Shell tips & tutorials direct to your Inbox.


Related posts