简体   繁体   中英

How to fetch change from Git using LibGit2Sharp?

The code below clone a Git url to a 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 fetch a new branch before merging it to local Git. Anyway, it throws An error was raised by libgit2. Category = Net (Error). Request failed with status code: 401 An error was raised by libgit2. Category = Net (Error). Request failed with status code: 401 An error was raised by libgit2. Category = Net (Error). Request failed with status code: 401 exception.

How to fix this?

您可以指定要通过FetchOptions实例使用的凭据作为Fetch调用的最后一个参数。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM