Skip to content

Instantly share code, notes, and snippets.

@ekampf
Created January 10, 2011 09:56
Show Gist options
  • Save ekampf/772597 to your computer and use it in GitHub Desktop.
Save ekampf/772597 to your computer and use it in GitHub Desktop.
Show the current Git branch in the command line prompt
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_COLOUR="\[\033[0m\]"
PS1="$GREEN\u@machine$NO_COLOUR:\w$YELLOW\$(parse_git_branch)$NO_COLOUR\$ "
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\]"
GRAY="\[\033[1;30m\]"
LIGHT_GRAY="\[\033[0;37m\]"
CYAN="\[\033[0;36m\]"
LIGHT_CYAN="\[\033[1;36m\]"
NO_COLOUR="\[\033[0m\]"
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.
[[ -r "$HOME/.rvm/scripts/completion" ]] && . "$HOME/.rvm/scripts/completion" # This adds rvm shell completion
PS1="$GREEN\u@machine$NO_COLOUR:\w:$LIGHT_GRAY\$(~/.rvm/bin/rvm-prompt i v g)$NO_COLOUR:$YELLOW\$(parse_git_branch)$NO_COLOUR\$ "
@kesavanm
Copy link

Thanks. Short & sweet. It works as expected.I customized with a minor change to fit my need.
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/|(\1)/'
Added a | to make the PS1 consistent with existing and trimmed the spaces

kmuthuvel@my-dev|~|2015-10-28 10:21:12
$cd /data/kmuthuvel/code
kmuthuvel@my-dev|/data/kmuthuvel/code|2015-10-28 10:21:23|(master)
$git checkout development
Switched to branch 'development'
kmuthuvel@my-dev|/data/kmuthuvel/code|2015-10-28 10:21:38|(development)
$cd
kmuthuvel@my-dev|~|2015-10-28 10:21:54
$echo $PS1
\[\033[0;32m\]\u@\h\[\033[0;33m\]|\w\[\e[35m\]|\e[34m\]\D{%F %T}\[\033[0;31m\]$(parse_git_branch)\[\033[0m\]\n$
kmuthuvel@my-dev|~|2015-10-28 10:21:55

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