Reputation: 1111
I would like to clone a given branch to local repository using LibGit2Sharp.
var repoPath = LibGit2Sharp.Repository.Clone("https://something", localpath, cloneOptions);
using (var repo = new LibGit2Sharp.Repository(repoPath))
{
var branches = repo.Branches.GetEnumerator();
}
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?
Actually, by default, Clone()
takes care of locally retrieving all commits of all branches. By default, only the remote HEAD branch (generally origin/master
), gets an automatically created local branch counterpart, which is then checked out.
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.
For instance, given you're interested in the branch my-feature-branch
and that your remote is named origin
:
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 => b.TrackedBranch = remoteBranch.CanonicalName);
Branch trackingBranch = repo.Branches["my-feature-branch"];
repo.Checkout(trackingBranch);
FWIW, there's a pending Pull Request to allow the user to explictly specify the branch one would like to see checked out.
EDIT
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.
var remoteBranch = repo.Branches["origin/" + branchName];
var newLocalBranch = repo.Branches.Add(branchName, commit, true);
repo.Branches.Update(newLocalBranch,
b => b.TrackedBranch = remoteBranch.CanonicalName);
var trackingBranch = repo.Branches[branchName];
repo.Checkout(trackingBranch, new LibGit2Sharp.CheckoutOptions(), author);
Upvotes: 5
Views: 4883
Reputation: 67639
Actually, by default, Clone()
takes care of locally retrieving all commits of all branches. By default, only the remote HEAD branch (generally origin/master
), gets an automatically created local branch counterpart, which is then checked out.
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.
For instance, given you're interested in the branch my-feature-branch
and that your remote is named origin
:
Branch remoteBranch = repo.Branches["origin/my-feature-branch"];
Branch newLocalBranch = repo.CreateBranch("my-feature-branch", remoteBranch.Tip);
// Make the local branch track the upstream one
repo.Branches.Update(newLocalBranch ,
b => b.TrackedBranch = remoteBranch.CanonicalName);
Branch trackingBranch = repo.Branches["my-feature-branch"];
repo.Checkout(trackingBranch);
FWIW, there's a pending Pull Request to allow the user to explictly specify the branch one would like to see checked out.
The Pull Request has been merged. Checking out a known branch after the Clone() call succeeded can now be done, more easily, through:
string clonedRepoPath = Repository.Clone(
url, targetPath,
new CloneOptions { BranchName = branchName });
Upvotes: 5