Skip to content

Instantly share code, notes, and snippets.

@danielalvarenga
Last active November 6, 2024 00:37
Show Gist options
  • Save danielalvarenga/2df8cabbd6f3041c2378 to your computer and use it in GitHub Desktop.
Save danielalvarenga/2df8cabbd6f3041c2378 to your computer and use it in GitHub Desktop.
Show branch in terminal Ubuntu
# Add in ~/.bashrc or ~/.bash_profile
function parse_git_branch () {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
RED="\[\033[01;31m\]"
YELLOW="\[\033[01;33m\]"
GREEN="\[\033[01;32m\]"
BLUE="\[\033[01;34m\]"
NO_COLOR="\[\033[00m\]"
# without host
PS1="$GREEN\u$NO_COLOR:$BLUE\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "
# with host
# PS1="$GREEN\u@\h$NO_COLOR:$BLUE\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "
@EdgarPozas
Copy link

Thanks!

@marwan2
Copy link

marwan2 commented Oct 28, 2024

@psk001 this worked for me on ubuntu 22.04
Add at the end of .bashrc file:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment