Making your bash prompt Git-aware (a.k.a. the superprompt)

I recently started on a project where I’m doing some pair programming and someone yesterday brought up the Pivotal Git scripts that help modify your Git local.name via a single command: git-pair [person1] [person2]. Then it was the case that another person said that they learned a trick of modifying their shell prompt to show the current local.name so you’d be reminded who you were working with—so you wouldn’t accidentally give attribution to someone you’re not pairing with. This turns out to be really helpful if you’re a freelancer like me who hops around different projects.

Later a nice addition was to show the current branch you’ve checked out. This is critical for me because I never remember and I’ve more than once created a new branch and forgotten to check it out.

And … today I was talking to some more devs and the request came up: “it’d be nice to know if I have modified files in my repo.”

So, I present to you a bunch of commands to add to your .bash_profile that do all of this:

function current_uname {
    uname -a | cut -d' ' -f2 | sed -e 's/..*$//'
}

function current_git_user {
    git config --get user.name
}

function current_git_branch {
    git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/; 1/'
}

function git_repo_has_modified_files {
    git status 2> /dev/null | sed -e 's/^#.*modified:.*$/*/' -e '/^#/d' -e '/^[^*]/d' | uniq
}

export PS1='$(current_uname) [$(current_git_user)$(current_git_branch)$(git_repo_has_modified_files)] A w > '
export PS2='  > '

Leave a Reply