Technotes for future me

Show current git branch with colors in Bash prompt

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

Restart terminal, or source ~/.bashrc.


Parse git branch

The parse_git_branch function will return the current git branch if we are currently in a git repo directory, otherwise it will return empty string.
In this function, git branch command will be used. This command will list of local git branches with the * symbol before the current branch:

[blaataap@dev:~/technote (master)]$ git branch
* master

If we are not in a git repo directory, the command will output something like this to stderr:

[blaataap@dev:~/technote (master)]$ git branch
fatal: Not a git repository (or any of the parent directories): .git

We surely don’t want this to appear in our prompt, so we redirect the stderr to /dev/null with this: 2> /dev/null
The last part is using sed command to extract the current branch name (the line starting with symbol *) from the git branch output.

git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'

Where -e '/^[^*]/d' will delete all lines that do not start with symbol *, and the -e 's/* \(.*\)/(\1)/' will replace (substitute) the current branch line (with symbol * and a space at the beginning) with the branch name, putting it in a pair of bracket ().

[blaataap@dev:~/technote (master)]$ git branch
* master
[blaataap@dev:~/technote (master)]$ git branch 2> /dev/null | sed -e '/^[^*]/d'
* master
[blaataap@dev:~/technote (master)]$ git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'

What is PS1 variable

PS1 stands for 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.

Example of a PS1 string:

blaataap@dev:~/technote $

To change this prompt, just set new value to PS1 string, for example: PS1='\u@\h \w > '

[blaataap@dev:~/technote (master)]$ PS1='\u@\h \w > '
blaataap@dev ~/technote >

Where \u, \h and \w are control commands for PS1 string, which stand for username of the current user, the hostname and the current working directory respectively.

How to set the colors

We use this sequence \[\e[32m\] to set the color of the text goes after that sequence, until another color is set. In this sequence, \[ and \] are used to mark the start and end of non-printing characters. Between them, \e denotes an ASCII escape character, and follows by [32m, which is the actual color code.

All the color codes can be referred here:

So, in our example: export PS1="\u@\h \[\e[32m\]\w \[\e[91m\]\$(parse_git_branch)\[\e[00m\]$ "

the command prompt will start with username@host and a space (\u@\h ), in default terminal color (normally, white text on black background).
Then comes \[\e[32m\]\w — current working directory (\w) in green ([32m) color, and a space.
Next is current git branch (\$(parse_git_branch)) if we are in a git repo, in light red ([91m) color.
The last part of the prompt is symbol $ in default ([00m) color. Final result is as following:

[blaataap@dev:~/technote (development)]$
[blaataap@dev:~/technote (master)]$


Last updated on 31 Jan 2021
Published on 23 Jul 2020
Edit on GitHub