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.
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
http://www.markus-gattol.name/ws/bash.html#git_bash_prompt
LikeLike
I have it on Mac as well. My colleague found out it is coming from .git-bash-completion.sh.
LikeLike
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/
LikeLike
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.
LikeLike
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 🙂
LikeLike