So now we have five variables with different colors, if our terminal supports it. Next up is defining the function that will poll Git for our current branch and color red or green it depending on whether our working directory is clean or not.

This Furthernction first checks to see whether we are in a git repository or not. The git rev-parse command terminates with an error exit code if not and we use that to crash and die early if there are no Git data.

Then it calls git branch and singles out the one you are at using the sed tool. This also works even if you are not on a branch, because then you are given “* (no branch)” in the branch list.

Finally, it runs git diff and works off of its exit code to determine if we have a working directory or not. Then we know what color to use for our branch.

By using exit codes like this, we speed up our data harvesting a lot. Running git status or git diff normally can take some time and we can not have that in our prompt.