Get the git-promt.sh
script here. Put it somewhere like ~/.git-prompt.sh
. If you want to be all CLI about it, you could just run:
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -o ~/.git-prompt.sh
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 ~/.git-prompt.sh
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:
Copied from this blog.. :-)