Code Worrier

Teaching myself to RTFM

Put Your Git Branch in Your Bash Prompt

| Comments

Lots of folks were interested in getting git autocomplete going in bash, so I thought I’d offer another useful git/bash tip. This one was suggested by Stephan in the comments of that earlier post.

If you’re using git properly and collaborating on a project, chances are you find yourself changing branches often. This can be quite disorienting. You deploy from the master branch, then get back to that bug fix you were about to work except for—whoops!—now you commited that fix to master and have to back away slowly. Confusions like this one can easily be avoided by putting you branch name front-and-center: at the end of your bash prompt. Here’s how.

Get the script here. Put it somewhere like ~/ If you want to be all CLI about it, you could just run:

curl -o ~/

Now modify your your bash profile (it’s at ~/.bash-profile, in case you’re new to this stuff). Before the part of the file that declares what your prompt will look like (PS1=[...]), load in the script you just downloaded, like so:

# Load in the git branch prompt script.
source ~/

Now add \$(__git_ps1) to your prompt declaration. You can put it wherever you like, but I think it’s best suited for the very end, right before the final dollar sign (\$) If you want to get all fancy, you can make it a pretty color. My declaration looks like this:

PS1="\[$GREEN\]\t\[$RED\]-\[$BLUE\]\u\[$YELLOW\]\[$YELLOW\]\w\[\033[m\]\[$MAGENTA\]\$(__git_ps1)\[$WHITE\]\$ "

(See here for color definitions.)

This declaration results in a prompt looks like this when I’m in the directory ~/src/food52 and on the branch develop:

That’s it. Never git lost again!