title: Automatically stashing
body: <p>The section <strong>Last links in the chain: Stashing and the reflog</strong> in <a href="http://ftp.newartisans.com/pub/git.from.bottom.up.pdf" rel="noreferrer">http://ftp.newartisans.com/pub/git.from.bottom.up.pdf</a> recommends stashing often to  take snapshots of your work in progress.  The author goes as far as recommending that you can use a cron job to stash your work regularly, without having to do a stash manually.</p>
  
  <blockquote>
    <p>The beauty of stash is that it lets you apply unobtrusive version control to your working
    process itself: namely, the various stages of your working tree from day to day. You can even use
    stash on a regular basis if you like, with something like the following snapshot script:</p>
  
  <pre><code>$ cat &lt;&lt;EOF &gt; /usr/local/bin/git-snapshot
  #!/bin/sh
  git stash &amp;&amp; git stash apply
  EOF
  $ chmod +x $_
  $ git snapshot
  </code></pre>
    
    <p>There’s no reason you couldn’t run this from a cron job every hour, along with running the reflog expire command every week or month.</p>
  </blockquote>
  
  <p>The problem with this approach is:</p>
  
  <ol>
  <li>If there are no changes to your working copy, the "git stash apply" will cause your last stash to be applied over your working copy. </li>
  <li>There could be race conditions between when the cron job executes and the user working on the working copy.  For example, "git stash" runs, then the user opens the file, then the script's "git stash apply" is executed.</li>
  </ol>
  
  <p>Does anybody have suggestions for making this automatic stashing work more reliably? </p>