title: Clone a given branch with LibGit2Sharp
body: <p>I would like to clone a given branch to local repository using LibGit2Sharp.</p>
  
  <pre><code> var repoPath = LibGit2Sharp.Repository.Clone("https://something", localpath, cloneOptions);
  
   using (var repo = new LibGit2Sharp.Repository(repoPath))
   {
       var branches = repo.Branches.GetEnumerator();
   }
  </code></pre>
  
  <p>With repo.Branches.GetEnumerator() I can see every remote branches, but with the Clone command I can clone only the master branch from GitHub? How can I clone the "testBranch" or something? </p>
  
  <p>Actually, by default, <code>Clone()</code> takes care of locally retrieving all commits of all branches. By default, only the remote HEAD branch (generally <code>origin/master</code>), gets an automatically created local branch counterpart, which is then checked out.</p>
  
  <p>So, once the clone is performed, all you have have got to do is creating a local branch from the remote branch you'd like to work this and check this newly created one out.</p>
  
  <p>For instance, given you're interested in the branch <code>my-feature-branch</code> and that your remote is named <code>origin</code>:</p>
  
  <pre><code>Branch remoteBranch = repo.Branches["origin/my-feature-branch"];
  
  Branch newLocalBranch = repo.CreateBranch("my-feature-branch");
  
  // Make the local branch track the upstream one
  repo.Branches.Update(newLocalBranch ,
       b =&gt; b.TrackedBranch = remoteBranch.CanonicalName);
  
  Branch trackingBranch = repo.Branches["my-feature-branch"];
  
  repo.Checkout(trackingBranch);
  </code></pre>
  
  <p>FWIW, there's a pending <strong><a href="https://github.com/libgit2/libgit2sharp/pull/650" rel="nofollow">Pull Request</a></strong> to allow the user to explictly specify the branch one would like to see checked out.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I updated my code with your suggestions, but it still not working well. The content of my local repository isn't equal with the trackingBranch, it's still representing the content of the master branch.</p>
  
  <pre><code>var remoteBranch = repo.Branches["origin/" + branchName];
  
  var newLocalBranch = repo.Branches.Add(branchName, commit, true);
  
  repo.Branches.Update(newLocalBranch, 
        b =&gt; b.TrackedBranch = remoteBranch.CanonicalName);
  
  var trackingBranch = repo.Branches[branchName];
  
  repo.Checkout(trackingBranch, new LibGit2Sharp.CheckoutOptions(), author);
  </code></pre>