How to get change from Git using LibGit2Sharp?

The code below clones the Git URL into the test directory.

var url = @"http://abc-555.com/team/project-555.git";
var path = @"E:\temp_555";

var credential =  new Credentials() { Username = "a8888", Password="88888888"};
var clonePath = Repository.Clone(url, path, credentials: credential);

using (var repo = new Repository(clonePath))
{
    foreach (var branch in repo.Branches)
    {
        Console.WriteLine(branch.Name);
    }

    // somebody creates a new branch here, so I want to fetch it.
    repo.Fetch("origin");

    foreach (var branch in repo.Branches)
    {
        Console.WriteLine(branch.Name);
    }
}

I want to get a new branch before merging it with local Git. In any case, this throws an exception An error was raised by libgit2. Category = Net (Error). Request failed with status code: 401.

How to fix it?

+3
source share
1 answer

You can specify the credentials that will be used with the FetchOptions instance as the last parameter . Fetch

repo.Fetch("origin", new FetchOptions { Credentials = credential});
+3
source

All Articles