title: LibGit2Sharp - Best way to stage and commit files
body: <p>I am trying to write a basic C# GIT function that commits and pushes a local working directory to its master repository. This is my first attempt at using GIT, and I am still trying to get my head around some of the basics.</p>
  
  <p>Here is my code. It's not very complex, all it needs to do is make sure that the repository is updated with any changes from my working directory. It works, but with a caveat as described below</p>
  
  <pre><code>public void Sync()
  {
      using (var repo = new Repository(@"C:\Temp\Git\test"))
      {
          repo.Stage("*");
          repo.Commit("This is my comment");
  
          Remote remote = repo.Network.Remotes["origin"];
          var options = new PushOptions();
  
          options.CredentialsProvider = new CredentialsHandler(
          (url, usernameFromUrl, types) =&gt;
              new UsernamePasswordCredentials()
              {
                  Username = "myusername",
                  Password = "mypassword"
              });
  
          var pushRefSpec = @"refs/heads/master";
          repo.Network.Push(remote, pushRefSpec, options, null, "push done...");
  
      }
  }
  </code></pre>
  
  <p>The problem I am facing is this. If I add or modify any files then they are updated accordingly in the repository. However if there are no files that need to be committed then the following exception is raised:</p>
  
  <pre><code>No changes; nothing to commit
  </code></pre>
  
  <p>What I would like to be able to do is determine if files need to be committed before attempting the commit operation, therefore being able to exit gracefully if nothing needs to be done. I.E something along the lines of:</p>
  
  <pre><code>if (there are files to commit)
  {
      repo.Commit("This is my comment");
  }
  </code></pre>
  
  <p>Does anyone know how to achieve this?</p>
  
  <p>As mentioned, this is my first attempt with GIT, so if there are better standards than my code above I would be very grateful if for any kind of advice.</p>
  
  <p>Many thanks and best regards</p>