« Quicksort golf3D printers - worry for the right reasons »

Mon, May 13, 2013

[Icon][Icon]Git prompt: Tell me more

• Post categories: Omni, FOSS, My Life, Programming, Helpful

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):

  • a * when there are unstaged changes, and a + when there are staged changes
  • a $ when there are stashed changes
  • a % when there are untracked files present


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.


Comment from: Carlo [Visitor]
Instead of writing a C prompt, you could give this a try, even if it's written in Python, it works pretty good for my needs.
17/05/13 @ 08:21
Comment from: oneandoneis2 [Member] · http://geekblog.oneandoneis2.org/
..you appear to have forgotten the link you intended to add? :)
17/05/13 @ 08:41

[Links][icon] My links

[Icon][Icon]About Me

[Icon][Icon]About this blog

[Icon][Icon]My /. profile

[Icon][Icon]My Wishlist


[FSF Associate Member]

September 2017
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  


User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

multiblog platform