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.
develop
branchThis 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 = "[email protected]";
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);
}
}
New Branches Git checkout works hand-in-hand with git branch . The git branch command can be used to create a new branch. When you want to start a new feature, you create a new branch off main using git branch new_branch . Once created you can then use git checkout new_branch to switch to that branch.
Difference between git checkout and git switch It can also be used to restore changes from a certain commit. But git checkout does more than that. It allows you to copy files from any branch or commit directly into your working tree without switching branches.
The git checkout command can be used in a commit, or file level scope. A file level checkout will change the file's contents to those of the specific commit. A revert is an operation that takes a specified commit and creates a new commit which inverses the specified commit.
git checkout -b BRANCH_NAME creates a new branch and checks out the new branch while git branch BRANCH_NAME creates a new branch but leaves you on the same branch. In other words git checkout -b BRANCH_NAME does the following for you.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With