DEV Community

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

Posted on • Edited on

How to add git branch name to shell prompt in Ubuntu

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 sample output result.

Before my shell prompt is like this. Note suppose I am on folder ~/general/repos/playground.

sony@ubuntu:~/general/repos/playground$
Enter fullscreen mode Exit fullscreen mode

We will create like this.

sony@ubuntu:~/general/repos/playground (master)$
Enter fullscreen mode Exit fullscreen mode

On above example, I am on my playground git repository directory and it display the branch name master. It means I am on master branch now.

Today I will share how to create it.

Scenario

I want to add git branch name to my shell prompt when I am in a project repository directory. I am using Ubuntu, so the default shell is bash.

Step-by-step to go there

Step 1. Go to your shell prompt first.

Step 2. We have to know the current prompt setting, type like below.

echo $PS1
Enter fullscreen mode Exit fullscreen mode

$PS1 is the enviroment variable that store the default prompt setting we see every time when we log in the console.

On mine, the setting is like below.

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
Enter fullscreen mode Exit fullscreen mode

Looks great.

Step 3. Open file ~/.bashrc

vi ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Step 4. Add this function on the bottom of your ~/.bashrc file.

git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
Enter fullscreen mode Exit fullscreen mode

Step 5. Add this on the bottom of your ~/.bashrc file. Add the git_branch function on the shell prompt, by adding it on PS1 variable. Sample like below (that based on my $PS1 output above).

export PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \$(git_branch)\$ "
Enter fullscreen mode Exit fullscreen mode

Step 6. You are done. Now save the file ~/.bashrc and run this command to reflect the changes.

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Or you can close the terminal and re-open again to reflect the changes.

Now on my shell prompt will display like below.

sony@ubuntu:~/general/repos/playground (master)$
Enter fullscreen mode Exit fullscreen mode

Add color to make it better

This is just additional cosmetics things. I will make the branch indicator (master) to green for foreground color. Add this \[\033[00;32m\]\$(git_branch)\[\033[00m\] to your PS1 variable. On mine is like below.

export PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \[\033[00;32m\]\$(git_branch)\[\033[00m\]\$ "
Enter fullscreen mode Exit fullscreen mode

That's it.

Thank you and I hope you enjoy it.

Reference

Credits

Top comments (14)

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 !