Quick tip: Adding the current git branch to the shell prompt

January 26, 2010

This information is readily available on the GitHub support pages, but I’m posting it here for reference (do I won’t have to dig it out again the next time I need it).

  1. Save this file somewhere and source it from your .bashrc or .bash_profile. This provides all sorts of completion magic for git, but I’m not really sure if it’s required for the prompt trick to work 🙂
  2. Update your PS1 to include __git_ps1. Example:
    export PS1='[ \u@\h \w$(__git_ps1 " (%s)") ]\$ '

Voila! You now see the current branch when CWD is inside a git repo:

[ jtj@nova ~/projects/eldorado (railsforum) ]$ 

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 )

Google photo

You are commenting using your Google 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: