Fastest way to get git status in bash

bash, git

Solution

Not entirely your answer, but bash-completion has this built-in.

If you set the bash ENV GIT_PS1_SHOWDIRTYSTATE to a nonempty value, unstaged (*) and staged (+) changes will be shown next to the branch name. You can configure this per-repository ith the bash.showDirtyState variable, which defaults to true once GIT_PS1_SHOWDIRTYSTATE is enabled.

You can also see if currently something is stashed, by setting GIT_PS1_SHOWSTASHSTATE to a nonempty value. If something is stashed, then a '$' will be shown next to the branch name.

If you would like to see if there are untracked files, then you can set GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there are untracked files, then a '%' will be shown next to the branch name.

Not sure about the speed degrade when you enable this though. If you want to do the coloring:

Staged files:

if git rev-parse --quiet --verify HEAD >/dev/null; then
 git diff-index --cached --quiet HEAD -- || color for staged changes
else
  color unstaged changes
fi

Stashed files

git rev-parse --verify refs/stash >/dev/null 2>&1 && color for stashed files

Untracked files

if [ -n "$(git ls-files --others --exclude-standard)" ]; then
  Color untrack files
fi

The above snippets come from the bash-completion script.

Problem

For a while now I've been using the `__git_ps1` function in my bash's PS1 prompt (with `PS1='\w$(__git_ps1)'`). Now I want to color it depending on branch status. I wrote a bash function that checks if the current branch is modified, and colors red or white depending on the status. The problem is that it uses `git status` to check the status (it's the only way I know off), and that's several times slower than `__git_ps1`, which is enough to cause an annoying delay when I'm using the prompt (I'm on a very weak netbook). So I ask: is there a faster way to check status of the current git folder? `__git_ps1` is a lot faster than manually parsing `git branch`, so I'm thinking there might be some other hidden git function.

Original source