Stiaannel
Blog

Displaying Github Branch in Linux Terminal

Make the following modification to your ~/.bashrc file.

# Comment Out the Old Prompt
# if [ "$color_prompt" = yes ]; then
# PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
# else
# PS1='${debian_chroot:+($debian_chroot)}\[email protected]\h:\w\$ '
# fi
# unset color_prompt force_color_prompt

# Get the git branch to display
parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

# Set New Prompt With The Git Branch

if [ "$color_prompt" = yes ]; then
  PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\] $(parse_git_branch)\[\033[00m\]\$ '
else
  PS1='${debian_chroot:+($debian_chroot)}\[email protected]\h:\w$(parse_git_branch)\$ '
fi
unset color_prompt force_color_prompt

Example of the outcome:

[email protected]:~/blog(main)$