Posts Tagged ‘linux’

Git Branch in Bash Prompt

January 8, 2011 Leave a comment

I like to work in text console and I’m usually using bash and git for my projects. When I have to switch between branches couple of times during the day and made changes I have to be careful which brach I have currently checked out.  It can be done quickly by just typing “git branch”, but  I don’t like to type it every time I need to do something.

One of the solution is displaying current git branch in bash prompt. My fouvrite one is as follows:

PS1='\[\e[32m\]\w `git branch 2>/dev/null | grep -e '\''^*'\'' | sed -E '\''s/^\* (.+)$/(\1) /'\''`\n\$\[\e[0m\] '

The interesting part is between “`” marks. This command lists all git branches, greps only the current one (with asterisk) and creates string with sed.

This prompt will not print anything about git branches if you are currently in the folder not handled by git repository. Normally above prompt will look like this:

But when you will enter directory under git repository it will look like this:

If I will modify my prompt in the future, you can find the latest version here.

Categories: linux, mac, source control Tags: , , ,