Show Your GIT Branch Name In Your Prompt

Typing git branch over and over to see what branch you are on sucks. Sure, you could argue that you should always KNOW what branch you're currently working on. And if you did, you would obviously not be a git user.

Bouncing around branches can be pretty common, and I know I've messed some things up pretty bad not knowing what branch I was on.

So set up your shell to always put the name of the current branch into your prompt.

function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
 
function proml {
  local        BLUE="\[\033[0;34m\]"
  local         RED="\[\033[0;31m\]"
  local   LIGHT_RED="\[\033[1;31m\]"
  local       GREEN="\[\033[0;32m\]"
  local LIGHT_GREEN="\[\033[1;32m\]"
  local       WHITE="\[\033[1;37m\]"
  local  LIGHT_GRAY="\[\033[0;37m\]"
  case $TERM in
    xterm*)
    TITLEBAR='\[\033]0;\u@\h:\w\007\]'
    ;;
    *)
    TITLEBAR=""
    ;;
  esac
 
PS1="${TITLEBAR}\
$BLUE[$RED\$(date +%H:%M)$BLUE]\
$BLUE[$RED\u@\h:\w$GREEN\$(parse_git_branch)$BLUE]\
$GREEN\$ "
PS2='> '
PS4='+ '
}
proml

Pastie Link

Put this at the top of your .bash_profile and you'll be pimping your branch all over.

Thanks to @defunkt for this.

Popularity: 43% [?]

You can skip to the end and leave a response. Pinging is currently not allowed.

16 Thoughts

  1. That has to be one of the coolest "why didn't I think of that" kind of things. Very nice...

  2. Awesome, thanks!

    With zsh, I believe you need to use precmd – I do:

    function precmd() { PROMPT="%n@%m ~$(parse_git_branch)# " }

    for output like

    henrik@Aether ~/Sites/somesite[master]%

    Obviously having defined your parse_git_branch function.

  3. That got a little messed up. See http://pastie.textmate.org/180088

  4. Rather than running git-branch + sed for every shell prompt, you can use this:

    function parse_git_branch
    {
    ref=$(git-symbolic-ref HEAD 2> /dev/null) || return
    echo ${ref#refs/heads/}
    }

  5. I got the majority of this script from somewhere on the web, can't remember where. It's only been tested with Bash. If you save it as .bash_vcs.sh you can load it in your .bash_profile with the line: source ~/.bash_vcs.sh.

    This script goes a little further then what you have above. It puts the current directory in the terminal header and checks whether the current directory is a git, svn or cvs working directory. If so, it changes the problem to look like this: ctwise:(git)grit[master]/$

    The prompt shows the type of source control system we're looking at (git), the project name (grit), the current branch (master) and the directory inside the source control area (/). It also sets appropriate aliases based on the source control system, e.g., 'pull' will map to 'git pull' in a git directory and 'svn up' in an svn directory. I can't take credit for the idea or the bulk of the implementation but can't remember where I found it.

    The script can be seen here: http://pastie.org/230805

  6. Thank you very much for this handy little snippet.

  7. It's orthogonal to the coolness of your script, but git status also tells you what branch you are on. And you're likely to be typing that a lot anyway.

  8. @ctwise your pastie has made my svn usage much more tolerable. I tweaked it a tiny bit to show non-basenamed paths when outside of a vcs tree. http://gist.github.com/3829/

  9. You might want to clear the foreground color to be the normal color at the end, I didn't like it as the same color as the $, green. You can add this ansi escape to clear it: \[33[0m\]

  10. Well, I liked it. But corrupt the bash history.

  11. If you use zsh, check out my prompt... http://www.jukie.net/~bart/blog/zsh-git-prompt

  12. Thank you bartman. I use bash, but yours scripts look greats and history seems not break (graphically) :)

    My bash ever corrupt the ouput when I change the prompt and then use history!

  13. Thanks a lot. Although produces a very long prompt, had to shorten it!

  14. This is a really great idea!

  15. Yeah, it's definitely useful. But at the risk of sounding trollish:

    http://github.com/rtomayko/git-sh/tree/master

    pwned.

    git-sh is AMAZING.

Share Your Thoughts?

Please excuse my captcha. But the internets require it these days. Comment triage isn’t billable.