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] $>
Written by cSquirrel
Related protips
6 Responses
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
Why not just use git branch -v ?
Short and sweet, nice!
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.
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.
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