Git tip: Show your branch name on the Linux prompt


Git is one of the most popular Source Control Management (SCM) software packages to provide revision control. Subversion (aka SVN) and Mercurial are other popular systems. These systems organize source code in branches and revisions that can be named by means of tagging. Branches can be used to allow you to work on different versions of the software concurrently. A popular way of organizing is to create a branch for every major feature you are building. Another organization can be used to distinguish between a “development” and “production” version, so that you can easily apply a small code change in case of any bugs.

In any of the above schemes it is important to know which branch you are working on, when committing code to the repository. This post explains how you can add the branch name in red to the prompt.

Firstly, we have to turn on the colored prompt in “.bashrc”:

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt

Now add the following code to “.bashrc” for Git branch information:

# Add git branch if its present to PS1
parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
if [ "$color_prompt" = yes ]; then
 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
unset color_prompt force_color_prompt

With this change you will be less likely to commit your code to the wrong branch. Enjoy!

PS: You can also show the branch name on the Linux prompt when using Mercurial (tip by Răzvan Tudorică).


13 thoughts on “Git tip: Show your branch name on the Linux prompt”

  1. @Willem-Jan: Thank you, that certainly is a good find and it sounds very promising! Where can I find the installation instructions to try it?

  2. I just saw the script changed a bit since I started using it.
    It is now split into 2 bash scripts, one for autocompletion and one for the updated prompt.

    – Completion script:
    – Prompt script:

    I found this blogpost that explains how to use it:
    But in the scripts itself it’s explained as well in the comments on top.

  3. @Willem-Jan: Thank you for the information. It is a valuable attribution to the post. I will try to use it ASAP.

  4. I primarily use RHEL/Centos/Amazon Linux servers and have my own version of this idea. It just colors the branch name and sticks it in brackets, which I prefer. I typically stick it at the end of the /etc/bashrc file.

    # Custom aliases and functions
    function parse_git_branch_and_add_brackets {
    git branch –no-color 2> /dev/null | sed -e ‘/^[^*]/d’ -e ‘s/* \(.*\)/\ \[\1\]/’
    PS1=”[\u@\h:\W]\[33[0;33m\]\$(parse_git_branch_and_add_brackets)\[33[0m\]\$ “

  5. @David: Thank you for your addition. That really helps for all people that run RedHat based Linux variants, great!

  6. In ~/.bashrc I uncommented force_color_prompt=yes and added that bit of code. Now I can see the current branch but the prompt is not coloured.

  7. This is enough for openSUSE (tested on 13.1 13.2 42.1)

    # Add git branch if its present to PS1
    parse_git_branch() {
    git branch 2> /dev/null | sed -e ‘/^[^*]/d’ -e ‘s/* \(.*\)/(\1)/’
    PS1=’${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ ‘
    unset color_prompt force_color_prompt

  8. @Admir: Thank you for visiting and commenting. Your input is highly appreciated! Kind regards, Maurits.

Leave a Reply

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