How to go from this…
… to this?
We will be changing your
.bashrc file. It is located in your home.
- Make a save of your
.bashrcfile (one is never too careful;
cp -p .bashrc .bashrc.sav).
- Optional step: make your prompt colored by uncommenting the highlighted line:
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
- Now locate the last line of the following excerpt and edit the previous lines accordingly to the following line:
# Add git branch if its present to PS1
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
if [ "$color_prompt" = yes ]; then
unset color_prompt force_color_prompt
- 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!