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

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

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