Add the current git branch to the Bash prompt


Git is a distributed version control system for tracking changes in source code during software development.

In general all the changes in the branches. It’s nice to see current branch in the prompt. It’s like a current working directory.

We need to customize your $PS1 – this shell variable is a template of Bash prompt.

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

You can add this variable definition to the file ~/.bashrc or ~/.bash_profile.

Note: we used escape sequences like we did it in previous note.

As a result you’ll see:

user@host ~/project (master) $