Colored prompt

How to show the Git branch in the Linux prompt

In pictures

How to go from this…

… to this?

Short answer

We will be changing your .bashrc file. It is located in your home.

  1. Make a save of your .bashrc file (one is never too careful; cp -p .bashrc .bashrc.sav).
  2. Optional step: make your prompt colored by uncommenting the highlighted line:

  1. Now locate the last line of the following excerpt and edit the previous lines accordingly to the following line:

  1. Restart your prompt, you’re done.

Something more detailed

I love Git command-line. It seems so much clearer than most (and less buggy than some) GUIs once you know how they work behind the scenes.

The installation comes with a Cygwin prompt. It is run inside the ugly Windows console, but still, it brings some color to it. And the current branch of the Git repository you’re in.

That’s why I was a bit disappointed when I installed it on my Ubuntu and discovered it did not display the branch without using git branch. And today, I was once more tricked because of it, so I hunted a solution.

And I found it on the web, customized to meet my preferences (which I recommend you do in turn). The sed part can be tuned (I added a space before the branch) and you can change the color.

Of course, you won’t have to see any change when you’re out of a repository.

Edit (2015-11-22): Alternative solution

You can also use liquidprompt to benefit from this feature and much more. Thanks to Jérémie for the tip!

Sources:

Published by

Cyrille Chopelet

Programming addict, UX philosopher, casual gamer, sci-fi enthusiast, hi-tech dilettante, ... Some people even call me a geek.

4 thoughts on “How to show the Git branch in the Linux prompt”

Leave a Reply