––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:
- Check out a branch
- Lunch!
- Get back to my desk and make an emergency bug fix
- Commit emergency fix
- 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 (master) $ git checkout myTag123 $ __git_ps1 ((myTag123)) |
(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:
- Merge something and there are a ton of conflicts
- Boldly decide to deal with it tomorrow and go home for the day
- The next day, write and emergency patch for something
- Check git status
- Suddenly I remember that I’m in the middle of a merge
- 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.
Subscribe