smhk

Git: display current branch in Bash

To show the current Git branch in Bash, e.g.:

user@host:/path/to/directory (branch_name)$

Append this to ~/.bashrc:1

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

PS1="$GREEN\u@\h$NO_COLOR:$BLUE\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "

Then either run source ~/.bashrc or reopen the shell to apply the changes.


  1. Taken from this earlier note, which in turn took this snippet from here. Thanks danielalvarenga and abdobouna↩︎