docs:git:show_current_branch_in_bash_prompt

show current branch in bash prompt

Modify your .bash_profile (or .profile) to include a bash function, then change your prompt:

# git related function to show the current branch with minimal system load;
# note that some may prefer to list this with git-symbolic-ref, but not all
# systems are set up that way, while git symbolic-ref should always work
function parse_git_branch {
	ref=$(git symbolic-ref HEAD 2> /dev/null) || return
	echo "("${ref#refs/heads/}")"
}
 
# example of my current prompt
PS1="\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$(parse_git_branch)\$ "

This is what it might look like:

username@hostname /path/to/some/project
(testing)$ 
  • docs/git/show_current_branch_in_bash_prompt.txt
  • Last modified: 2011/12/14 15:32
  • by billh