What is equivalent of git checkout branch_name on LibGit2Sharp

I have 2 branches in git server which are master and develop. I clone the repository to my machine using the code below.

var options =
    new CloneOptions()
    {
        CredentialsProvider = this.credentialsHandler,
    };

return
    Repository.Clone(
        gitHttpUrl,
        localPath,
        options);

The default branch is master. I want to switch to develop branch with the following code.

using (var repo = new Repository(localPath))
{
    var branch = repo.Branches[branchName];

    repo.Checkout(branch);
}

but branch is null.

How can I switch the branch using LibGit2Sharp?

Update
The default branch is master and below are the steps to reproduce the problem.

  1. Pull
  2. Checkout develop branch
  3. Pull, it throws an exception here.

This is the code:

private void Checkout(string localPath, string branchName)
{
    using (var repo = new Repository(localPath))
    {
        var branch = repo.Branches[branchName];

        if (branch == null)
        {
            branch = repo.CreateBranch(branchName, "origin/" + branchName);
        }

        repo.Checkout(branch);
    }
}

private MergeResult Pull(string path)
{
    const string NAME = "test-user";
    const string EMAIL = "test-email@gmail.com";

    using (var repo = new Repository(path))
    {
        var merger =
            new Signature(
                NAME,
                EMAIL,
                DateTimeOffset.UtcNow);

        var options =
            new PullOptions
            {
                FetchOptions = new FetchOptions()
                {
                    CredentialsProvider = this.credentialsHandler
                }
            };

        return repo.Network.Pull(merger, options);
    }
}

  • git archive with LibGit2Sharp
  • How to find all commits having a particular parent?
  • Programmatically delete directory of cloned repository
  • LibGit2Sharp Renamed files
  • How to get file's contents on Git using LibGit2Sharp?
  • Is any body making a windows replacement to msysgit with libgit2,libgit2sharp, Ngit?
  • Git notes perfomance and alternatives
  • LibGit2Sharp equivalent for git describe --contains
  • One Solution collect form web for “What is equivalent of git checkout branch_name on LibGit2Sharp”

    Similarly to Git, when cloning, LibGit2sharp will only create a local branch for the remote HEAD

    All others branches will be fetched as well and accessible as remote tracking branches, but no local branch will be automatically created for them.

    Thus, in order to achieve your goal, you can either checkout the remote tracking branch (eg. origin/master). Note, that, in this case, the HEAD will be automatically set in detached mode.

    repo.Checkout["origin/my_branch"];
    

    Or, create a local branch from the remote tracking one, then checking out the local one.

    var branch = repo.CreateBranch("my_branch", "origin/my_branch");
    
    repo.Checkout(branch);
    
    Git Baby is a git and github fan, let's start git clone.