7

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);
    }
}
Anonymous
  • 9,366
  • 22
  • 83
  • 133

1 Answers1

7

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);
nulltoken
  • 64,429
  • 20
  • 138
  • 130
  • The second parameter of repo.CreateBranch method accepts `Commit` or `committish`. How can I pass in a remote branch name? – Anonymous Sep 30 '14 at 11:47
  • A `committish` is anything that can point to a `commit`. This method supports the **[extended SHA1 syntax](https://www.kernel.org/pub/software/scm/git/docs/gitrevisions.html)**. Thus, passing `"origin/your_branch_name"` will work.` – nulltoken Sep 30 '14 at 11:58
  • I updated my question to include code example. I tried passing `"origin/" + branchName` but it didn't help. – Anonymous Sep 30 '14 at 12:14
  • Seeing your code, it looks like the original question regarding `checkout` has been answered, hasn't it? – nulltoken Sep 30 '14 at 12:46
  • Regarding the update to your question, it may benefit from a proper question of its own as it seems like a different topic. When you'll create it, could you please include the exception/inner exception details? – nulltoken Sep 30 '14 at 12:47
  • 1
    When I call `Checkout(branchName, "origin\" + branchName)`, it does not link a tracking branch for it which causes exception in later steps. – Anonymous Oct 01 '14 at 02:53
  • As explained in the question, checking out the remote branch with detach the HEAD. So you have first to **create** the local branch from the remote tracking one, then **checking out** the **local** branch. – nulltoken Oct 01 '14 at 07:36