Blended Cocoa

Adventures in Objective-C, Swift and Cocoa

Bash Prompt With Git Branch

For a while I have been using a Bash Terminal Prompt which is normally green but turns red if the last process failed. I have just discovered a prompt which includes the current git branch and I have updated my prompt to include both of these functions.

My terminal prompt now looks like the following screen capture (I’m currently using the Solarized Light theme):

You can add the following snippet to your .bash_profile, .profile or .bashrc file to get the same prompt:

.bash_profileView Gist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function parse_git_branch {
        git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \[\1\]/'
}

function prompt {
        local DEFAULT="\[\033[0m\]"
        local RED="\[\033[0;31m\]"
        local GREEN="\[\033[0;32m\]"
        local BLUE="\[\033[0;34m\]"

        PS1="\`if [ \$? == '0' ]; then echo '$GREEN'; else echo '$RED'; fi\`\h:\W \u$BLUE\$(parse_git_branch)$DEFAULT\$ "
}

prompt

The most up-to-date version of this prompt is available as a Gist at https://gist.github.com/4122721

Comments