DEV Community

Cover image for How to add git branch name to shell prompt in Ubuntu

How to add git branch name to shell prompt in Ubuntu

Sony AK on January 11, 2020

When I work in a git repository directory that has many branches, I want to know which branch I am currently in on my shell prompt. Below is the sa...
Collapse
 
bruno2kd profile image
Bruno Kern

I want to add a * if there are not staged file changes and a + if there are staged file changes. I used to have that on my old computer and want to do on my new one. Do you have any idea on how I can do it?

Thanks!

Collapse
 
edgarvazquez43 profile image
edgarVazquez43 • Edited

Maybe it help you, add these lines to .bashrc :

# Colors definition
BLUE='\e[94m'
L_BLUE='\e[96m'
YELLOW='\e[93m'
GREEN='\e[32m'
L_GREEN='\e[92m'
RED='\e[31m'
L_RED='\e[91m'
NC='\e[0m'

export GIT_PS1_SHOWDIRTYSTATE=true        # show * for changes
export GIT_PS1_SHOWUNTRACKEDFILES=true    # show % for new files
export PS1="$GREEN\u$YELLOW@$L_GREEN\h$YELLOW: $BLUE\W $L_RED\$(__git_ps1)$NC $ "
Enter fullscreen mode Exit fullscreen mode
Collapse
 
otreblanc profile image
otreblanc

Thanks

Collapse
 
ameernormie profile image
Ameer Hamza

Awesome article. One gotcha though, If I make a new branch from the terminal it doesn't immediately reflect the changes. I have to open a new terminal or run source ~/.bashrc again

Collapse
 
sonyarianto profile image
Sony AK

Thanks for the comment, yes yes you are right. I update also the article.

Collapse
 
sawzarnilinhtay profile image
Zarni

Awesome!

Collapse
 
sonyarianto profile image
Sony AK

Thank you!

Collapse
 
danble profile image
Danble

Thanks, man! :)

Collapse
 
raquelsartwork profile image
Raquel Santos | RS-coding

I am adding at the end of the file the git branch function. and how do I get out of the file ~/.bashrc, to continue the tutorial and write the next code on shell prompt?

Collapse
 
sawzarnilinhtay profile image
Zarni

How to change color for git branch name??? Please! defalut color is white, i wanna change color.

Collapse
 
sonyarianto profile image
Sony AK

I think on the article already explained :)

Collapse
 
chientv90 profile image
Chien

It works for me. Thank you!
How to change branch name to other color? (for example: red,purple)

Collapse
 
sonyarianto profile image
Sony AK

Hi Chien, thanks for reading my article, for changing to other color please refer to color code, for example in this link misc.flogisoft.com/bash/tip_colors... or this shellhacks.com/bash-colors/

Collapse
 
dylangrums profile image
DylanGrums

Awesome ! Thank you for this nice article !