Show Git dirty state (and branch) in the prompt

Written . Tagged Git, Shell scripting.

I’ve been using this excellent hack for a while to get the current git branch in my shell prompt.

That takes care of knowing what branch you’re on, but I still found myself running git status often after cd:ing into a working directory, to find out if it was dirty (had uncommitted stuff). If it is, I want to handle that before starting on something else.

I hacked that in, and now my prompt looks like e.g.

henrik@Hyperion ~/dev/blog.johannaost.com[master]$

when the working directory is clean, and

henrik@Hyperion ~/dev/blog.johannaost.com[master*]$

when it’s dirty – an asterisk is added.

Code for bash, goes into ~/.bashrc:

I have nearly no experience in shell scripting, so please let me know how this can be improved. For one thing, I expect it could be made more efficient by just running git status once and getting both branch and dirtiness from that.

Feel free to contribute versions for other shells.