title: Nodegit - How do I stage and commit a file? (simple example!)
body: <p>Using Node-git I just want to:</p>
  
  <ol>
  <li>Open a repo (where a file has been written/updated)</li>
  <li>Stage the file</li>
  <li>Do commit</li>
  </ol>
  
  <p>Using the git cli I would write something like this</p>
  
  <pre><code>cd repo    
  git add file.js
  git commit -m "Added file.js"
  </code></pre>
  
  <p>I'm trying to follow the examples <a href="https://github.com/nodegit/nodegit/blob/master/examples/add-and-commit.js" rel="noreferrer">here</a> describing how to do it with nodegit but have a hard time following these lines of code:</p>
  
  <pre><code>.then(function() {
    return repo.refreshIndex();
  })
  .then(function(indexResult) {
    index = indexResult;
  })
  .then(function() {
    // this file is in the root of the directory and doesn't need a full path
    return index.addByPath(fileName);
  })
  .then(function() {
    // this file is in a subdirectory and can use a relative path
    return index.addByPath(path.join(directoryName, fileName));
  })
  .then(function() {
    // this will write both files to the index
    return index.write();
  })
  .then(function() {
    return index.writeTree();
  })
  .then(function(oidResult) {
    oid = oidResult;
    return nodegit.Reference.nameToId(repo, "HEAD");
  })
  .then(function(head) {
    return repo.getCommit(head);
  })
  .then(function(parent) {
    var author = nodegit.Signature.create("Scott Chacon",
      "schacon@gmail.com", 123456789, 60);
    var committer = nodegit.Signature.create("Scott A Chacon",
      "scott@github.com", 987654321, 90);
  
    return repo.createCommit("HEAD", author, committer, "message", oid, [parent]);
  })
  .done(function(commitId) {
    console.log("New Commit: ", commitId);
  });
  </code></pre>
  
  <p>Does it have to be so long?
  What are the roles of <strong>repo.refreshIndex(), index.write(), index.writeTree()</strong> etc. etc.? The <a href="http://www.nodegit.org/api/" rel="noreferrer">API-docs</a> is not so beginner friendly. </p>
  
  <p>Thankful for enlightenment!</p>