Show Git Branch in Bash Terminal

by Melvin Ram

via Bedazzle Your Bash Prompt with Git Info // RailsTips by John Nunemaker.

I came across the above rails tip by John Nunemaker today. It shows how you can show which git branch you’re on while in your bash terminal.

The code he provided didn’t exactly work for me (I’m on OSX) so I ended up changing things a little bit:

function parse_git_branch {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

GREEN="\[33[0;32m\]"
YELLOW="\[33[0;33m\]"
WHITE="\[33[0;0m\]"

PS1="$GREEN\$(date +%H:%M) \w$YELLOW \$(parse_git_branch)$WHITE\$ "

This produces a bash that looks like the screenshot below. As you’ve probably guessed, jetpack is the git repo/directory and authorization is the branch I’m currently on.

Git repo on authorization branch

{ 6 comments… read them below or add one }

Melvin Ram May 20, 2009 at 10:06 pm

Hmm that’s interesting. I wonder why that is. When I put in what you have, the colors don’t work for me but with the original code, it works. In anycase, glad you got it working.

Will others pls comment on which of the two (if any) works for you? Also add your OSX version. I am on Leopard.

Robby Colvin May 20, 2009 at 5:26 pm

The colors aren't working for me. I had to do this to make it work:

GREEN=”[33[0;32m]“
YELLOW=”[33[0;33m]“
WHITE=”[33[0;0m]“

melvinram May 20, 2009 at 10:07 pm

Hmm that’s interesting. I wonder why that is. When I put in what you have, the colors don’t work for me but with the original code, it works. In anycase, glad you got it working.

Will others pls comment on which of the two (if any) works for you? Also add your OSX version. I am on Leopard.

Ashley Williams May 21, 2009 at 12:30 pm

Here's an alternative one:
http://pastie.org/485245

Which outputs:
http://www.grabup.com/uploads/06fbca0fb8cbb751c...

(the asterisk shows if the branch has un-commited changes)

lucho December 10, 2009 at 8:56 am

both codes are wrong. linux

Melvin Ram December 11, 2009 at 7:24 pm

Lucho – I say it works on OSX, not linux so that might be the difference. If you do figure out what works on linux, I’d be happy to add that to the post.

Leave a Comment