Plasticx Blog

Capable of being shaped or formed

Vebose Git Dirty Prompt

Posted by Mike 10/23/2009 at 01:22PM

I’ve updated my bashrc with a verbose dirty git prompt which is an extension of Henrik Nyh’s Show Git dirty state post. I wanted my prompt to also indicate additional git states with a single character. For instance I want to know when the local branch is ahead of the remote branch and I do that with the plus ‘+’ character. So if there are modified files in the project, and the local project is ahead of the remote I’ll see ‘☭’ for dirty and ‘+’ for ahead in my bash prompt such as the following

mike@daisy 10006 ~/projects/apps/example(master☭?)$

These are the character codes I used for the git dirty state in the project.

  • ‘☭’ – files have been modified
  • ‘?’ – there are untracted files in the project
  • ‘*’ – a new file has been add to the project but not committed
  • ‘+’ – the local project is ahead of the remote
  • ‘>’ – file has been moved or renamed

Here is an example in an example git repository

The code for my git dirty prompt is in this Gist and listed below.

  1. origin of work
    function parse_git_dirty {
    status=`git status 2> /dev/null`
    dirty=` echo -n “${status}” 2> /dev/null | grep -q “Changed but not updated” 2> /dev/null; echo “$?”`
    untracked=`echo -n “${status}” 2> /dev/null | grep -q “Untracked files” 2> /dev/null; echo “$?”`
    ahead=` echo -n “${status}” 2> /dev/null | grep -q “Your branch is ahead of” 2> /dev/null; echo “$?”`
    newfile=` echo -n “${status}” 2> /dev/null | grep -q “new file:” 2> /dev/null; echo “$?”`
    renamed=` echo -n “${status}” 2> /dev/null | grep -q “renamed:” 2> /dev/null; echo “$?”`
    if [ “${dirty}” == “0” ]; then
    if [ “${untracked}” == “0” ]; then
    if [ “${newfile}” == “0” ]; then
    if [ “${ahead}” == “0” ]; then
    if [ “${renamed}” == “0” ]; then
    echo “${bits}”

function parse_git_branch {
git branch —no-color 2> /dev/null | sed -e ‘/^[^*]/d’ -e “s/* \(.*\)/(\1$(parse_git_dirty))/”

export PS1=’\[\033[00;32m\]\u\[\033[01m\]@\[\033[00;36m\]\h\[\033[01m\] \! \[\033[00;35m\]\w\[\033[00m\]\[\033[01;30m\]$(parse_git_branch)\[\033[00m\]\$ ’

Posted in |


Use the following link to trackback from your own site:

Web Statistics