Daniel Lubarov

Simple bash prompt with git info

This gives a simple prompt with the information I consider most useful. There's not much code, so it's easy to customize.

fg_black=$(tput setaf 0)
fg_red=$(tput setaf 1)
fg_green=$(tput setaf 2)
fg_yellow=$(tput setaf 3)
fg_blue=$(tput setaf 4)
fg_magenta=$(tput setaf 5)
fg_cyan=$(tput setaf 6)
fg_white=$(tput setaf 7)
none=$(tput sgr0)

function in_git_repo {
  [ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1
}

function git_current_branch {
  in_git_repo && ref=$(git symbolic-ref HEAD) && echo " ("${ref#refs/heads/}")"
}

function git_flags {
  if in_git_repo; then
    # If you're working in a large repo and the prompt is slow to appear, try commenting out the next line.
    test -z "$(git ls-files --exclude-standard --others)" || echo -n '+' # Untracked files
    git diff-index --quiet --cached HEAD || echo -n '@' # Staged changes
    git diff-files --quiet || echo -n '*' # Changes in working tree
  fi
}

export PS1="\[$fg_green\][\w]\[$fg_yellow\]\$(git_current_branch)\[$fg_red\]\$(git_flags) \[$fg_black\]\$ \[$none\]"

Here's how it looks:

[~/Development/daniel] (master)+* $