Skip to content
vpfau edited this page Nov 7, 2018 · 6 revisions

git-fetch

Fetch updates from a remote

Git

$ git fetch origin

LibGit2Sharp new version (using Commands)

string logMessage = "";
using (var repo = new Repository("path/to/your/repo"))
{
    var remote = repo.Network.Remotes["origin"];
    var refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);
    Commands.Fetch(repo, remote.Name, refSpecs, null, logMessage);
}
Console.WriteLine(logMessage);

LibGit2Sharp old version

using (var repo = new Repository("path/to/your/repo"))
{
    Remote remote = repo.Network.Remotes["origin"];
    repo.Network.Fetch(remote);
}

Fetch all remotes, using authentication

Git

$ git fetch --all

LibGit2Sharp new version (using Commands)

string logMessage = "";
using (var repo = new Repository("path/to/your/repo"))
{
	FetchOptions options = new FetchOptions();
	options.CredentialsProvider = new CredentialsHandler((url, usernameFromUrl, types) => 
		new UsernamePasswordCredentials() 
		{
			Username = "USERNAME",
			Password = "PASSWORD"
		});

	foreach (Remote remote in repo.Network.Remotes)
	{
		IEnumerable<string> refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);
		Commands.Fetch(repo, remote.Name, refSpecs, options, logMessage);
	}
}
Console.WriteLine(logMessage);

LibGit2Sharp old version

using (var repo = new Repository("path/to/your/repo"))
{
    foreach(Remote remote in repo.Network.Remotes)
    {
        FetchOptions options = new FetchOptions();
        options.CredentialsProvider = new CredentialsHandler(
            (url, usernameFromUrl, types) => 
                new UsernamePasswordCredentials() 
                {
                    Username = "USERNAME",
                    Password = "PASSWORD"
                });
        repo.Network.Fetch(remote, options);
    }
}