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.
- Pull
- Checkout
develop
branch - 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);
}
}