Git Command Prompt

A super-useful little .bash_profile tweak to bring context to git and git-flow

I take no credit for this genius little bit of code but if you're using git, or the brilliant git-flow here's a few lines to add to your .bash_profile to bring you command-line usage of git to life.

Basically all it does it stick the current branch into your command prompt for you.

spirit@develop $ git flow feature start foo

To get this fired up just add the following four lines to your bash profile.  You can find this in ~/.bash_profile

parse_git_branch() {
      git branch --no-color 2> /dev/null | fgrep '*' | sed 's/* //'
export PS1="\W@$(parse_git_branch) $ "

And there you have it.  

Next time you log in you'll have a new command prompt.  To load up the new prompt right away you can use 'source' (Thanks to @zackkitzmiller for showing me this bit).

# source ~/.bash_profile 

Done? Great.  

Credit to @philsturgeon for finding this script.


Write a comment.

Responses. (1)

  1. D R



    Note: If you already have a ~/.bashrc file, the additions will need to go here rather than ~/.bash_profile.

    This does modify my prompt, but doesn't seem to pick up the git branch; it just leaves a space after the @ symbol. (Ubuntu 10.04, Git