Home > linux, mac, source control > Git Branch in Bash Prompt

Git Branch in Bash Prompt

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: , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: