My colleagues know my love for some simple branch identification in your shell prompt when using Git. It’s a foreign concept until you start using a decentralised SCM then all of a sudden you’re wandering in the Enchanted Wood and you can’t see the trunk for all the branches.

I’ve always used Lachie Cox’s magic colours version of Tim Lucas’s Straight-to-OSX version of this Debian mojo.

And there’s the thing. This is so useful that it just keeps evolving, hopscotching from blog post to tumblelog to pastie to gist.

In that vein, I’ve assembled my own patchwork quilt of bash for laying under all those branches, which helps you avoid the dirt by making it easy to see.

I smooshed this cunning work from Henrik Nyh into Lachie’s one-liner and added a dash of stylistic flair from Geoffrey Grosenbach.

# Combining Lachie Cox's crazy Git branch mojo:
# with
# AND Geoff Grosenbach's style:
# Sweeeeeeeet!
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "(☠)"
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* (.*)/1$(parse_git_dirty)/"
export PS1='[33[01;32m]w $(git branch &>/dev/null; if [ $? -eq 0 ];
then echo "[33[01;34m]$(parse_git_branch)"; fi) $ [33[00m]'

I’m guessing the bash scripting here is about as strained as my analogies, so I’d welcome some tips (and/or an entire re-write) from those who have some (indeed, any) clue.

And the result still has pretty colours, a clear identification of which branch you’re on and, best of all, a skull that appears when you’re in a dirty state.

The metaphorical dirt of the Enchanted Wood is, of course, a state of mind. All it really means is don’t go playing with other branches until you’ve finished everything on your plate (or index).

Fresh ideas, announcements, and inspiration for your team, delivered weekly.

Subscribe now