title: git: programmatically test if index is dirty
body: <p>I am writing a script which will do this:</p>
  
  <pre><code>#!/bin/bash
  mysqldump -u user -p database &gt; db_file
  git add db_file
  git commit -m 'db_file updated by script'
  </code></pre>
  
  <p>However, what if the index is dirty when I run this script? That is, what if I already <code>git add</code>ed files that I want to not have automatically committed when this script runs? I could do this:</p>
  
  <pre><code>#!/bin/bash
  mysqldump -u user -p database &gt; db_file
  git stash
  git add db_file
  git commit -m 'db_file updated by script'
  git stash apply
  </code></pre>
  
  <p>But now the problem is that if the index and working tree is <em>not</em> dirty, then git stash doesn't add anything to the stash list, but <code>git stash apply</code> will incorrectly apply whatever is on top of the stash and discard the history of that stash.</p>
  
  <p>How can I make a bash conditional based on if the index is dirty?</p>