How to: Display the current Git branch in your prompt
Git is the best version control system I’ve worked with (so far). I started feeling this way once I got the hang of branching. But if you have a lot of branches lying around, it’s easy to get lost. Fortunately, it’s easy to fix that problem, by pimping your terminal prompt. Mine now looks like this:
And suddenly, keeping track of branches has become easy.
There are many blog posts floating around the internet that explain how to make your prompt look like this, and this post is but one of them. What makes mine different, is that it has the following features:
- It works on both Ubuntu and OS X.
The first git prompt that I used, turned out not to work on OS X for some reason. This one does. - Dirty flag
If you’ve made a change, a * will be added to the name of the branch, showing you have outgoing changes. - Merge conflicts flag
If you have merge conflicts, a big yellow |MERGING will be added to the name of the branch. - A no email setting flag
I have no global user.email setting, because I want to push to work repos and my personal GitHub from the same machine, and not accidentally use the wrong email address. However, occasionally I would forget to set my email in a new repository, and push garbage to it. No more! This flag will remind me. - Colors to recognise different pieces of information at a single
glance
Although it does kind of look like a christmas tree this way. Fortunately, the colors are easily changed.
Here’s the code for my prompt. Save it as ~/fancy-prompt.sh
and call it from
your ~/.bashrc
like this:
The dot at the start of the line is important; it won’t work if you leave it out!
Or better yet: add it to your Dropbox!