Git Pro Tip: Show your branch on Linux Ubuntu terminal

 If you would like to add branch name to bash prompt then we have to edit the PS1variable(set value of PS1 in~/.bash_profile).

Want to know about PS1 ?

PS1 denotes Prompt String 1. Which is one of the prompt available in Linux/UNIX shell. If we open our terminal, it displays the content defined in PS1 variable in your bash prompt.

Here is an example of how to edit “.bashrc” for Git branch information.

Open up your terminal and type.

nano ~/.bashrc

Note: use sudo command as per you need, otherwise make sure your current user has proper permission to save .bashrc file.

Now, scroll up to last and paste the code snippet provided below.

then press, Ctrl + O to save, Ctrl + X to close the nano edit mode, then restart the terminal.

parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
export PS1="\[email protected]\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

Enjoy !