|« Quicksort golf||3D printers - worry for the right reasons »|
Mon, May 13, 2013
tl;dr - The Bash __git_ps1 git prompt does more if you set a few environment variables
If you're a command-line devotee, you'll be aware of tab-completion.
And if you're a git user, I really hope you're aware that it comes with its own tab completion files, which allows it to auto-complete not just commands, but branch names and other such helpful things.
If you're not, you'll find it in git's contrib/completion/ directory. If you're a bash user, copy the git-completion.bash script to /etc/bash_completion.d/git
The nice thing about this script is that it also comes with a helpful functionality that you can insert into your PS1 - the environment variable that defines what your command prompt should look like.
To add this, add the string \$(__git_ps1 \"(%s)\") to your $PS1:
djh@devel $ echo $PS1 \u@\h $ djh@devel $ export PS1="\u@\h \$(__git_ps1 \"(%s)\")$ " djh@devel (develop)$ echo $PS1 \u@\h $(__git_ps1 "(%s)")$ djh@devel (develop)$
And yay, I now have the current branch on display in my command prompt!
There's more to it than that - the prompt also tells you when you're in the middle of a rebase, or a merge; it shows you when you're in a detached head state.. all kinds of things.
All this is pretty well known, and most git-and-bash users know about and use this stuff. (Zsh users typically seem to have their own bewildering variety of all-singing, all-dancing, multi-coloured git PS1 functionality)
But what I didn't know until this weekend, when I trawled through the source code (don't ask) is that the humble Bash git prompt actually offers a little more functionality than you might think - functionality which wasn't mentioned in any of the guides I used to set up the PS1 prompt.
Because there's a few environment variables that you can define which will add to the amount of info displayed by the prompt:
If you set these vars to a non-empty value, you'll get (respectively):
djh@devel (develop)$ export GIT_PS1_SHOWDIRTYSTATE=1 djh@devel (develop *+)$ export GIT_PS1_SHOWSTASHSTATE=1 djh@devel (develop *+$)$ export GIT_PS1_SHOWUNTRACKEDFILES=1 djh@devel (develop *+$%)$
(Yes, I had a rather dirty checkout at the time of writing :)
I'm giving serious thought to writing my own Git prompt in C, that'll show me other such useful things as the number of staged/unstaged/untracked files - more as a learning exercise than out of any belief that someone else hasn't already done so.
But in the meantime, this is a surprisingly helpful addition to what I always thought of as a brilliant, but somewhat bare-bones, bash function.
|<< <||> >>|