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?

asked Oct 7, 2015 by Eric Z Ma (44,280 points)

1 Answer

 
Best answer

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
answered Oct 7, 2015 by Eric Z Ma (44,280 points)
selected Oct 15, 2015 by Eric Z Ma

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...