title: How to hard reset to previous commit in libgit2?
body: <p>My program (which uses libgit2) makes a commit to local repo (no push, only commit), then does <em>something</em> and then, according to the result, tries to revert the last commit by resetting repo to HEAD~1 (one commit before HEAD). Simply hard reset.</p>
  <p><strong>The problem</strong>: the code below does reset HEAD to HEAD~1, but leaves all changes between these 2 commit as &quot;staged&quot;, while I need to simply erase these changes. What am I doing wrong?</p>
  <pre class="lang-cpp prettyprint-override"><code>bool RevertLastCommit(std::string &amp;errorMessage) 
  {
      int error;
      git_object *head_commit_obj = nullptr;
      error = git_revparse_single(&amp;head_commit_obj, repo, &quot;HEAD~1&quot;);
  
      if (error &lt; 0)
      {
          return false;
      }
  
      git_commit *head_commit = (git_commit *)head_commit_obj;
  
      git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
      opts.checkout_strategy = GIT_CHECKOUT_FORCE;
      
      int reset_result = git_reset(repo, const_cast&lt;git_object *&gt;(head_commit_obj), GIT_RESET_HARD, &amp;opts);
      
      if (reset_result &lt; 0) {
          printf(&quot;Reset failed!\n&quot;);
          return false;
      }
  
      printf(&quot;Done reverting\n&quot;);
  
      error = git_checkout_head(repo, &amp;opts);
      if (error &lt; 0)
      {
          return false;
      }
  
      printf(&quot;Hard checkout\n&quot;);
      return true;
  }
  
  </code></pre>