How to show the current GIT branch name in command prompt

git-branches

We can get the current GIT branch name in command prompt with adding a simple code into the .bashrc file.

Open your terminal and type the following command:

sudo nano ~/.bashrc

Go to the end of the file and add this code:

function parse_git_branch () {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
 
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
NO_COLOR="\[\033[0m\]"
 
PS1="$GREEN\u@\h$NO_COLOR:\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "

Save file changes.

Now, if you open a new terminal and access into a GIT repository, you will see the current GIT branch name.