How to test whether the git repository is dirty?

How to test whether the git repository is dirty?

git status can show this. But how to diagrammatically detect this in bash?

This piece of bash code works like a charm for me:

[[ $(git diff --shortstat 2> /dev/null | tail -n1) != "" ]]

You can use it to build your script.

For example, print “dirty” or “clean”:

if [[ $(git diff --shortstat 2> /dev/null | tail -n1) != "" ]]; then
  echo "dirty"
else
  echo "clean"
fi

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *