title: How to catch a "fatal index.lock: file exists" error when calling git checkout in a shell script?
body: <p>The shell-script is creating files for a Flyway delta release, which applies all changes for a new release into an Oracle database.</p>
  
  <ol>
  <li>"git checkout" new release-tag with all the new changes</li>
  <li>Check that returncode of the "git checkout" call is ok</li>
  <li>Retrieve a list of all files, which have changed between the releases</li>
  <li>Files, which have changed are basically copied to a new directory and receive a generated prefix for Flyway2</li>
  </ol>
  
  <p>Later on those files can be easily applied with Flyway...</p>
  
  <p>Problem that happened recently: Step 1 might fail due to a "fatal: Unable to create '.../.git/index.lock': File exists." error (Most likely due to a status update by SourceTree).</p>
  
  <p>Unfortunately git's returncode is 0 in this case (meaning no error), which means the script does not notice, that the files were <strong>not</strong> updated in step 2 and goes on with steps 3 and 4 (but with wrong files).</p>
  
  <p>In this example I try to circumvent the problem by waiting until the "index.lock" is removed, however this is not fail safe! There might be a index.lock created right after the while-loop is passed, but before the git checkout and I have the same problem again.</p>
  
  <pre><code>GIT="Path to Git.exe"
  INDEXLOCK="Path to potential .git/index.lock"
  
  #Unfortunately git checkout returns 0 (everything is ok), when it failed due to an existing index.lock
  #The script would then commence, although the sources were not checkout correctly!
  #Loop until index.lock does not exist anymore
  while [ -f "$INDEXLOCK" ]
  do
      echo "Index.lock exists! Waiting until open git process is finished."
      sleep 5
  done
  
  echo " "
  echo "Trying to check out tag: $2"
  $GIT checkout "$2" -q
  if [ $? = 1 ]; then
      echo "Error: Can't checkout tag: $2"
      exit;
  fi
  echo "Check out finished"
  </code></pre>
  
  <p>Is there a possibilty to make sure the shell-script stops on a ".git/index.lock" error?</p>