Last Updated: February 25, 2016
·
2.96K
· csquirrel

Git: See current branch name in terminal prompt

Add the following to your ~.bash_profile

function git-branch-name {
  git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3-8
}
function git-branch-prompt {
  local branch=`git-branch-name`
  if [ $branch ]; then printf " [%s]" $branch; fi
}
PS1="\u@\h \[\033[0;36m\]\W\[\033[0m\]\[\033[0;32m\]\$(git-branch-prompt)\[\033[0m\] \$ "

Expect result

[feature/git_branch_name] $>

6 Responses
Add your response

You may want to check the __git_ps1 bash function distributed with git:
http://stackoverflow.com/questions/4133904/ps1-line-with-git-current-branch-and-colors

over 1 year ago ·

Why not just use git branch -v ?

over 1 year ago ·

Short and sweet, nice!

over 1 year ago ·

Why not just use git branch -v ?
... to get the current branch name?

Can't see a big improvement using it.
This or other way you have to process the output to extract branch name.

over 1 year ago ·

You may want to check the _gitps1 bash function distributed with git

AFAIK it is not part of every git distribution and as such is not available on every system.
This snippet does work on every git system.

over 1 year ago ·

AFAIK, the recommended way of showing branch (and commit status) is using gitprompt='$(__git_ps1 "[%s]")'

more details: http://www.freshblurbs.com/blog/2011/03/25/developers-ultimate-bash-prompt.html

over 1 year ago ·