title: Un-stage file with libgit2
body: <p>Using objective-git and libgit2 it has been fairly easy to stage a file ready for commit:</p>
  
  <pre><code>GTIndex *repoIndex = [self.repository indexWithError:&amp;error];
  
  [repoIndex removeFile:path error:&amp;error];
  
  if (status != GTFileStatusIgnored &amp;&amp; status != GTFileStatusWorkingDeleted) {
      // Now add the file to the index
      [repoIndex addFile:path error:&amp;error];
  }
  
  [repoIndex write:&amp;error];
  </code></pre>
  
  <p>However un-staging a file is proving to be a tad more tricky. Simply removing it from the repository's index doesn't work as git then thinks the file has been deleted which makes sense. It seems what I need to do is change the index entry in the index to the one it was before it was staged.</p>
  
  <p>I have tried doing the following, using diff to get the old diff delta and constructing a <code>git_index_entry</code> from that and inserting it:</p>
  
  <pre><code>GTIndex *repoIndex = [self.repository indexWithError:&amp;error];
  GTBranch *localBranch = [self.repository currentBranchWithError:&amp;error];
  GTCommit *localCommit = [localBranch targetCommitAndReturnError:&amp;error];
  
  GTDiff *indexCommitDiff = [GTDiff diffIndexFromTree:localCommit.tree inRepository:self.repository options:nil error:&amp;error];
  
  // Enumerate through the diff deltas until we get to the file we want to unstage
  [indexCommitDiff enumerateDeltasUsingBlock:^(GTDiffDelta *delta, BOOL *stop) {
  
      NSString *deltaPath = delta.newFile.path;
  
      // Check if it matches the path we want to usntage
      if ([deltaPath isEqualToString:path]) {
          GTDiffFile *oldFile = delta.oldFile;
  
          NSString *oldFileSHA = oldFile.OID.SHA;
          git_oid oldFileOID;
          int status = git_oid_fromstr(&amp;oldFileOID, oldFileSHA.fileSystemRepresentation);
  
          git_index_entry entry;
          entry.mode = oldFile.mode;
          entry.oid = oldFileOID;
          entry.path = oldFile.path.fileSystemRepresentation;
  
          [repoIndex removeFile:path error:&amp;error];
  
          status = git_index_add(repoIndex.git_index, &amp;entry);
  
          [repoIndex write:&amp;error];
      }
  }];
  </code></pre>
  
  <p>However this leaves the git index in a corrupt state resulting in any git command logging a fatal error:</p>
  
  <pre><code>fatal: Unknown index entry format bfff0000
  fatal: Unknown index entry format bfff0000     
  </code></pre>
  
  <p>What is the correct way to un-stage a file using libgit2?</p>