Garrick
Garrick

Reputation: 71

How to push single commit to remote branch libgit2sharp

I'm trying to push a single specific commit to a remote branch using LibGit2Sharp.

I'm looking for the equivalent of git push <remote name> <commit hash>:<remote branch name> https://miteshshah.github.io/linux/git/how-to-push-single-commit-with-git/

Is there anything similar for LibGit2Sharp? Or is there a better approach for this?

Thanks,

Garrick

Upvotes: 1

Views: 1113

Answers (1)

Jim Aho
Jim Aho

Reputation: 11957

Have you looked at their documentation? According to that you should be able to use:

using (var repo = new Repository("path/to/your/repo"))
{
    LibGit2Sharp.PushOptions options = new LibGit2Sharp.PushOptions();
    options.CredentialsProvider = new CredentialsHandler(
        (url, usernameFromUrl, types) =>
            new UsernamePasswordCredentials()
            {
                Username = USERNAME,
                Password = PASSWORD
            });
    repo.Network.Push(repo.Branches[BRANCHNAME], options);
}

Another variant, using Remote to push to the origin:

using (var repo = new Repository("path/to/your/repo"))
{
    Remote remote = repo.Network.Remotes["origin"];
    var options = new PushOptions();
    options.CredentialsProvider = (_url, _user, _cred) => 
        new UsernamePasswordCredentials { Username = "USERNAME", Password = "PASSWORD" };
    repo.Network.Push(remote, @"refs/heads/master", options);
}

Upvotes: 2

Related Questions