––thursday #3: a handy git prompt

Everyone says to use git branches early and often, but I inevitably lose track of what branch I’m on. My workflow generally goes something like:

  1. Check out a branch
  2. Lunch!
  3. Get back to my desk and make an emergency bug fix
  4. Commit emergency fix
  5. Suddenly realize I’m not on the branch I meant to be on, optimally (but not always) before I try to push

To keep track, I’ve modified my command prompt to display what I’m working on using __git_ps1, which prints a nice “you are here” string for your prompt:

$ __git_ps1
$ git checkout myTag123
$ __git_ps1

(Newlines added for clarity, it actually displays as ” (master)$ git checkout…”, which is generally what you want so your prompt doesn’t contain a newline.) I’m not sure what’s up with all the ‘()’s, but it does let you distinguish between branches (‘(branchname)’) and tags (‘((tagname))’).

__git_ps1 will even show you if you’re in the middle of a bisect or a merge, which is another common workflow for me:

  1. Merge something and there are a ton of conflicts
  2. Boldly decide to deal with it tomorrow and go home for the day
  3. The next day, write and emergency patch for something
  4. Check git status
  5. Suddenly I remember that I’m in the middle of a merge
  6. Do unpleasant surgery to get my git repo back to a sane state

If you tend to fall into a similar workflow, I highly recommend modifying your prompt to something like:

$ PS1='\w$(__git_ps1)> '
~/gitroot/mongo (v2.0)>

And suddenly life is better, which is what Linux is all about.

You can run PS1=... on a per-shell basis (or export PS1 for all shells this session) or add that line to your ~/.bashrc file to get that prompt in all future shells.

Finally, __git_ps1 isn’t a binary in your path, it’s a function (so it won’t show up if you run which __git_ps1). You can see its source by running type __git_ps1.

Note: unfortunately this doesn’t work for me on OS X, so I think it might be Linux-only.

  • Markus Gattol

    If on Debian and bash-completion package is installed then there’s a way to just uncomment a few lines and get this for free too

  • Guest

     I have it on Mac as well. My colleague found out it is coming from .git-bash-completion.sh.

  • Here’s a fairly extensive colorizing one: https://gist.github.com/1878135
    I personally use one included in RVM, since I do a fair amount of context switching. This provides not only the git branch, but also current status and interpreter in use. Less relevant to non-ruby users, but still useful. http://beginrescueend.com/workflow/prompt/

  • Thanks for posting this. I’ve been using this git prompt in windows powershell: https://github.com/dahlbyk/posh-git Its honestly a little slow, but the auto complete is very useful.

  • Hi, I’m slowpoke, but… 🙂

    If you installed git (git-core) from MacPorts, then try
        $ . /opt/local/share/doc/git-core/contrib/completion/git-completion.bash

    I just added this to my ~/.profile file, thanks for useful tip 🙂

kristina chodorow's blog