fasnya
Last Updated: June 19, 2019
·
218.8K
· itseranga

Add git branch name to bash prompt

In order to add branch name to bash prompt we have to edit the PS1 variable(set value of PS1 in ~/.bash_profile).

What is PS1

PS1 denotes Prompt String 1. It is the one of the prompt available in Linux/UNIX shell. When you open your terminal, it will display the content defined in PS1 variable in your bash prompt

Following is an example bash prompt of my terminal. In here I have defined to display
* user - eranga
* host name of the machine - erangas-MacBook-Pro-4
* current working directory - ~/Workspace/wasn

Picture

More details about bash prompt and PS1

Display git branch name

Add following lines to your ~/.bash_profile

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

In here parse_git_branch() function extract the branch name when your are in git repository. This function output used in PS1 variable in order to prompt the branch name.

In above PS1 we defined following properties
* \u@\h \[\033[32m\] - user, host name and its displaying color
* \w\[\033[33m\] - current working directory and its displaying color
* \$(parse_git_branch)\[\033[00m\] - git branch name and its displaying color

Now when you go to git repository form the terminal it will display currently checked out git branch in the prompt. Following is the example output of bash prompt after adding these changes to ~/.bash_profile

Picture

18 Responses
Add your response

15808

Or you can use oh-my-zsh and stay forever happy with themes. :)

over 1 year ago ·
16143
29061

Make sure that you escape the $ at the end of the PS1 line, or it will nuke the colors of your session.

over 1 year ago ·
29277

Or you can use a helper tool like https://github.com/magicmonty/bash-git-prompt which also shows you the current status of the repository ;-)

over 1 year ago ·
29890

You can see the how to guides on the http://techola.net/

over 1 year ago ·
30131
30132
30915

https://www.premiuminfo.org Get all tricks and tricks.

9 months ago ·
31267

Just wanted to point that colors actually go before what you want to color :
[\033[32m]\w is what makes the working directory \w green

8 months ago ·
31860
31884
31917

NICE : https://blogzia.com #TrysomethingNew

5 months ago ·
32218

Store of Tech Information https://www.techmint.in/

4 months ago ·
32373
32387
32401

Nice! In addition to the branch name, To show branch status (dirty/ahead/behind/stashed content,etc..), you can use the git-prompt ;)
http://digitalfortress.tech/tutorial/setting-up-git-prompt-step-by-step/

3 months ago ·
32535
33684

Universiade: Games will represent Italy as a whole says De Luca. Napoli 2019 welcomes the planet to the Summer Universiade's 60th birthday. https://livesportworld.com/category/summer-universiade/ Sitting just south of Rome, Napoli has been ready to unite the dynamism of university. The 2019 Summer Universiade, the XXX Summer Universiade (Italian: Universiadi estive 2019) will be held in Naples, Italy, between 3 and 14 July 2019. Campania Governor Vincenzo De Luca,

27 days ago ·