According to: https://thucnc.medium.com/how-to-show-current-git-branch-with-colors-in-bash-prompt-380d05a24745
Make these settings in ~/.bashrc
:
- Add function to get current git branch:
parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' }
- Search variable
PS1
and add this part before the prompt$
:This sets color-code red and returns the output of the\[\e[91m\]\$(parse_git_branch)\[\e[00m\]
parse_git_branch
function. Beware of quotes!
Result:
# ~/.bashrc
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
...
if [ "$color_prompt" = yes ]; then
PS1="${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\e[91m\]\$(parse_git_branch)\[\e[00m\]\$ "
else
PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w\[\e[91m\]\$(parse_git_branch)\[\e[00m\]\$ "
fi