How do I get my iTerm prompt to display differently when I'm in a Git branch?

purinkle picture purinkle · Mar 31, 2011 · Viewed 48.2k times · Source

I'm trying to get my iTerm prompt set up the same way as Paul Irish

So far I have the following in ~/.profile:

# Add git branch name to prompt
parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/*\(.*\)/ on \1/'
}

PS1='\n\[\033[0:35m\]\u\[\033[0;32m\]\w\[033[0m\]$(parse_git_branch)\n\$\[\033[0m\] '

I don't know how to make just the branch appear in a different colour and not the preceding "on"

As well as this there are other features such as:

  • Displaying an "o" at the prompt when not in a git branch
  • Displaying a "±" when in a branch
  • Displaying the date at the end of the line

Any help would be appreciated

Answer

spong picture spong · Apr 1, 2013

I use git-aware-prompt.

A lot of solutions I had before only displayed the git branch if I were only in that directory when the terminal loaded. If I started iTerm in a non-git repo, then it wouldn't work when I cd into directory with a git repo.

This github project solved that for me.