How do I show the git branch with colours in Bash prompt?

309,187

Solution 1

This snippet:

# 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\]\$ '
else
 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi

Is meant to replace the default prompt definition:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

Which ends with:

unset color_prompt force_color_prompt

The .bashrc you posted shows you're adding it after the default prompt definition and unset color_prompt force_color_prompt (line #64).

Either replace the default prompt definition with the snippet or leave your ~/.bashrc as it is and comment the default prompt definition along with unset color_prompt force_color_prompt on line #64:


So part of your .bashrc could look like

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\]\$ '
else
 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi
# THE SIX LINES BELOW are the default prompt and the unset (which were in the original .bashrc)
#if [ "$color_prompt" = yes ]; then
#    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
#else
#    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
#fi
#unset color_prompt force_color_prompt

screensot

Solution 2

Ubuntu: Show your branch name on your terminal

Add these lines in your ~/.bashrc file

# Show git branch name
force_color_prompt=yes
color_prompt=yes
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\]\$ '
else
 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi
unset color_prompt force_color_prompt

Reload the .bashrc file with this command:

$ source ~/.bashrc

Solution 3

For now, I followed this https://gist.github.com/eliotsykes/47516b877f5a4f7cd52f and working, liking it so far, though I'm planning to customize it further.

In Terminal

mkdir ~/.bash

Copy the raw git-prompt.sh file from git contrib in to the ~/.bash directory: https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

Inside ~/.bashrc or ~/.bash_profile (choose the file where you normally put any bash customizations/setup), add the lines:

source ~/.bash/git-prompt.sh # Show git branch name at command prompt
export GIT_PS1_SHOWCOLORHINTS=true # Option for git-prompt.sh to show branch name in color

# Terminal Prompt:
# Include git branch, use PROMPT_COMMAND (not PS1) to get color output (see git-prompt.sh for more)
export PROMPT_COMMAND='__git_ps1 "\w" "\n\\\$ "' # Git branch (relies on git-prompt.sh)

As long as you're inside a git repo, your Bash prompt should now show the current git branch in color signifying if its got uncommitted changes.

Solution 4

Quick hack:

  1. Adding this to ~/.bashrc:
parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

export PS1="\u@\h \[\e[32m\]\w \[\e[91m\]\$(parse_git_branch)\[\e[00m\]$ "
  1. Restart the terminal, or source ~/.bashrc:

enter image description here

More detail: https://medium.com/@thucnc/how-to-show-current-git-branch-with-colors-in-bash-prompt-380d05a24745

Solution 5

Append the lines below to ~/.bashrc:

export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true

export PS1='\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '
Share:
309,187

Related videos on Youtube

u123
Author by

u123

Updated on September 18, 2022

Comments

  • u123
    u123 over 1 year

    I am using this guide to show the branch name in gnome terminal (Ubuntu 15.10) when working in a git repository. Based on the above I now have the below in my ~/.bashrc file:

    # 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
    #force_color_prompt=yes 
    

    ...

    # 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\]\$ '
    else
     PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
    fi
    unset color_prompt force_color_prompt
    

    As a result I now get:

    enter image description here

    so it works. But why has the coloring of my user@host been removed? And I would also expect that the branch name should be colored. Before it looked like this:

    enter image description here

    UPDATE: I have now tried this guide instead:

    https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-prompt

    adding this to .bashrc:

    parse_git_branch() {
         git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
    }
    export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
    

    and that works:

    enter image description here

    Notice in .bashrc I also have this (default):

    # 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
    #force_color_prompt=yes
    

    I have yet to find the reason why that snippet gives the correct result and the other version does not. Any input on this?

    Here is the version of my .bashrc that has the old snippet enabled that does not work:

    http://pastebin.com/M8kjEiH3

    • muru
      muru about 8 years
      Was force_color_prompt uncommented before?
    • u123
      u123 about 8 years
      Yes I have tried with both uncommented and commented same result. The guide posted above says its should be commented out.
    • muru
      muru about 8 years
      Can you post your complete .bashrc? IIRC the default .bashrc doesn't enable colour prompts, so you have to change it to show colours. It depends on what you changed.
    • muru
      muru about 8 years
      Your new snippet has colour codes in them (the \[\033[32m\] and similar parts) without any checks. The old snippet checks if colour prompts are enabled before using them, and if they're not enabled, well.
    • u123
      u123 about 8 years
      As far as I can see the old snippet also has that when it evals to true. And I did try with 'force_color_prompt=yes' enabled/disabled. I made sure to restart gnome-terminal after each change.
    • muru
      muru about 8 years
      That's why I asked you to show the rest of the .bashrc.
    • muru
      muru about 8 years
      Have a look at line 64, which should tell you why uncommenting force_color_prompt didn't help.
    • u123
      u123 about 8 years
      I have added a link to a pastebin of the bashrc file containing the version of the old snippet that does not work.
    • kos
      kos about 8 years
      @u123 The .bashrc you just posted confirms my theory, you're setting the prompt after unset color_prompt force_color_prompt. Remove line 64 and it'll work.
    • u123
      u123 about 8 years
      Thanks that works. I never touched this part of my .bashrc file. Assumed that the old snippet would work without having to modify other parts of the bashrc file. I will stick with the new snippet since it works with a default .bashrc file - no need for removing line 64.
    • muru
      muru about 8 years
      @u123 don't worry about the default .bashrc too much. If you mess up, you can always get the original from /etc/skel/.bashrc.
    • jjjjjj
      jjjjjj over 5 years
      How can I add this only if the current directory is a git repo? I tried an if/else based on whether or not the contents of $git_parse_branch() were empty, but this didn't work
    • djvg
      djvg over 5 years
      For anyone wondering about the color codes and other magic symbols...
    • Player1
      Player1 over 4 years
      Thank you! I love it when deploying.
    • CervEd
      CervEd almost 3 years
      I'd recommend using git-prompt.sh instead. Nevertheless, there's no need to use git branch and pipe to sed. Just use git describe --contains --all HEAD
  • u123
    u123 about 8 years
    Verified the above and you are correct. I will stick with new version of snippet since it works without having to modify the default parts of the bashrc file.
  • Avinash Raj
    Avinash Raj about 8 years
    it fails to add color to the branch name.
  • kos
    kos about 8 years
    @AvinashRaj Test it with a copy of the default ~/.bashrc in /etc/skel/.bashrc, you might have something interfering in your ~/.bashrc.
  • Adil Abbasi
    Adil Abbasi almost 7 years
    un-comment force_color_prompt=yes (line # 48) if the colors are not visible.
  • Elazar
    Elazar over 6 years
    Faster: git symbolic-ref --short HEAD 2> /dev/null
  • Niket Pathak
    Niket Pathak over 6 years
    To color your branch according to its status, you can use the native git-prompt script provided by git itself.
  • user2018197
    user2018197 about 6 years
    With this method, if you are using any python virtual environment, keep in mind that this won't display the virtual environment name in the terminal.
  • Denis Stephanov
    Denis Stephanov over 5 years
    Hello, I tried it and it works only where I switch superuser, can you tell me how to enable always?
  • cbloss793
    cbloss793 over 5 years
    This worked for me on 18.04!
  • Bishwas Mishra
    Bishwas Mishra over 5 years
    Thanks. Using: Ubuntu 18.04.1 LTS
  • call0fcode
    call0fcode over 5 years
    It worked also for me on elementaryOS 0.4 Loki. As simple as open my ~/.bashrc with the command sudo nano ~/.bashrc, copy your code at the end of the file, save it, exit and reload the ~/.bashrc with the code you pasted above. Thanks a lot ;)
  • BeeGee
    BeeGee about 5 years
    Works on Redhat 6.8!
  • warkentien2
    warkentien2 about 5 years
    This worked and kept the colors! (Ubuntu 18.04)
  • miguelmorin
    miguelmorin almost 5 years
    This should be the accepted answer, as it's clear, concise, and does the job, and it works on other platforms too.
  • Piotr Żak
    Piotr Żak over 4 years
    Wokred on ubuntu 19.04
  • fanny
    fanny over 4 years
    works on ubuntu 18.04
  • Manuel Abascal
    Manuel Abascal over 4 years
    I'm using Ubuntu 18.04.3 LTS & Terminator console and it works like a charm!
  • Grávuj Miklós Henrich
    Grávuj Miklós Henrich over 4 years
    Works on Ubuntu 18.04.3 LTS
  • Shantanu Tomar
    Shantanu Tomar about 4 years
    Worked for me in Ubuntu 19.10
  • nilon
    nilon about 4 years
    How could I change the color and/or length of the git branch name that shows?
  • Divyesh Prajapati
    Divyesh Prajapati about 4 years
    Working in 18.04
  • Santiago
    Santiago almost 4 years
    Works well for me and also shows the conda environment, when it's not the default one or when I go from another env into the default one.
  • Riyafa Abdul Hameed
    Riyafa Abdul Hameed almost 4 years
    Created a video to show the use of the git-prompt script created by git itself youtu.be/6DczjqG9HbA
  • Emanuel Lindström
    Emanuel Lindström over 3 years
    Works fine for Ubuntu 20.04 on WSL2. Thanks!
  • michal
    michal over 3 years
    On Ubuntu 20.10 working correctly ;)
  • Danilo Roascio
    Danilo Roascio over 3 years
    The following at the end of ~/.bashrc simply appends to the distribution's default color prompt export PROMPT_COMMAND='__git_ps1 "'${PS1%???}'" "\n\\\$ "'
  • Ajit Panigrahi
    Ajit Panigrahi about 3 years
    I had this in my centOS & RHEL VMs and worked there, but for Ubuntu this didn't update on switching the current directory. Had to follow this for setting and unsetting values
  • Robert Benson
    Robert Benson almost 3 years
    You should not need to use sudo to modify your own login file
  • Javier
    Javier almost 3 years
    Worked perfectly on 20.04 LTS
  • Albert Hidalgo
    Albert Hidalgo almost 3 years
    Worked on Ubuntu 20.04.2 LTS (WSL)
  • igor
    igor almost 3 years
    I am not an expert, but managed to simplify parse method: git branch --show-current 2> /dev/null | sed -e 's/\(.*\)/ (\1)/'. Does anyone see any issues with this?
  • igor
    igor almost 3 years
    Indeed, git branch --show-current 2> /dev/null | sed -e 's/\(.*\)/ (\1)/' works for me.
  • suhailvs
    suhailvs over 2 years
    do i need to comment line unset color_prompt force_color_prompt or not?
  • Abdes
    Abdes over 2 years
    Worked perfectly on Debian GNU/Linux 9.13
  • Rajan
    Rajan over 2 years
    Please always try to provide the full answer even if you you are trying to improve someone else's answer. :)
  • Oli
    Oli over 2 years
    I think it's visually apparent from the question that this probably isn't the case here, but it may apply to others.
  • pintu
    pintu over 2 years
    Its working perfectly on ubuntu20.04 LTS
  • infiniteLearner
    infiniteLearner about 2 years
    Awesome, working!!!!
  • Admin
    Admin almost 2 years
    @nilon To change the color of the branch name, replace 01;31m in the square brackets before $(parse_git_branch) with 33m, which is much more pleasent for the eyes, especially when using the shell with purple background. List of colors here: cyberciti.biz/faq/…
  • Admin
    Admin almost 2 years
    @Jakob Note that 01;33m is for yellow even though your link says it's brown