loading...
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

sonyarianto profile image Sony AK Updated on ・2 min read

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$

We will create like this.

sony@ubuntu:~/general/repos/playground (master)$

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

$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\]\$

Looks great.

Step 3. Open file ~/.bashrc

vi ~/.bashrc

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)/'
}

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)\$ "

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

source ~/.bashrc

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)$

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\]\$ "

That's it.

Thank you and I hope you enjoy it.

Reference

Credits

Posted on by:

Discussion

pic
Editor guide
 

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

 

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

 
 

Thank you!

 

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

 

I think on the article already explained :)

 

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

 

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/

 

Awesome ! Thank you for this nice article !