Set Git Branch In Your Bash Prompt

I use git for my source code management and branch regularly.

It’s good to know which branch your on when your performing command line tasks. You can run git branch, but that gets tedious.

You can add a function to your .profile (or .bash_profile etc) to get a prompt that looks like: (master)$

This outputs the current user (denoted by \u) followed by the current directory (\W) and finally the git branch inside parentheses.

When you’re not in a git repository the prompt omits the parentheses and git branch.

Show me the code