title: How to check if there's nothing to be committed in the current branch?
body: <p>The goal is to get an unambiguous status that can be evaluated in a shell command.</p>
  <p>I tried <code>git status</code> but it always returns 0, even if there are items to commit.</p>
  <pre class="lang-bash prettyprint-override"><code>git status
  echo $?  #this is always 0
  </code></pre>
  <p>I have an idea but I think it is rather a bad idea.</p>
  <pre class="lang-bash prettyprint-override"><code>if [ git status | grep -i -c &quot;[a-z]&quot;&gt; 2 ];
  then
   code for change...
  else
    code for nothing change...
  fi
  </code></pre>
  <p>any other way?</p>
  <hr />
  <p><strong>update with following solve, see Mark Longair's post</strong></p>
  <p>I tried this but it causes a problem.</p>
  <pre class="lang-bash prettyprint-override"><code>if [ -z $(git status --porcelain) ];
  then
      echo &quot;IT IS CLEAN&quot;
  else
      echo &quot;PLEASE COMMIT YOUR CHANGE FIRST!!!&quot;
      echo git status
  fi
  </code></pre>
  <p>I get the following error <code>[: ??: binary operator expected</code></p>
  <p>now, I am looking at the man and try the git diff.</p>
  <p>===================code for my hope, and hope better answer======================</p>
  <pre class="lang-bash prettyprint-override"><code>#if [ `git status | grep -i -c &quot;$&quot;` -lt 3 ];
  # change to below code,although the above code is simple, but I think it is not strict logical
  if [ `git diff --cached --exit-code HEAD^ &gt; /dev/null &amp;&amp; (git ls-files --other --exclude-standard --directory | grep -c -v '/$')` ];
  then
          echo &quot;PLEASE COMMIT YOUR CHANGE FIRST!!!&quot;
      exit 1
  
  else
      exit 0
  fi
  </code></pre>