Search code examples
gitgit-branchgit-clonelibgit2sharprepo

libgit2sharp branch not found on freshly cloned repo


libgit2sharp 0.21.0.176 C# on Windows Server 2008

I need to obtain reference to a specific branch.

On my Windows machine, I fresh git clone local repo from remote origin on GitHub.

Next I execute following libgit2sharp code :

var localRepo = new Repository({local-path},RepositoryOptions);
var remote = localRepo.Network.Remotes.Add("origin", {remote-repo-uri);
localRepo.Network.Fetch(remote, FetchOptions, Signature, "performing fetch");
var branch = localRepo.Branches[{branch-name}];

But the branch is not present in the collection even though it exists on remote origin on GitHub.


Solution

  • Similarly to git, only the default branch (or the one that has been specified in CloneOptions.BranchName) is created as a local head upon cloning.

    Other branches are fetched, but only as remote tracking branches.

    Two options:

    • Given your remote is named "origin", you can access those remote tracking branches throughlocalRepo.Branches["origin/{branch-name}"];
    • You create a local branch and configure it to track its remote counterpart. See BranchFixture.CanSetUpstreamBranch() test for an example of how to do this.