如何将单个提交推送到远程分支 libgit2sharp

时间:2021-04-16 20:57:48

标签: libgit2 libgit2sharp

我正在尝试使用 LibGit2Sharp 将单个特定提交推送到远程分支。

我正在寻找相当于 { "id": 3, "balanceToItem": [] } https://miteshshah.github.io/linux/git/how-to-push-single-commit-with-git/

LibGit2Sharp 有没有类似的东西?或者有更好的方法吗?

谢谢,

加里克

1 个答案:

答案 0 :(得分:1)

你看过their documentation吗?据此,您应该可以使用:

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);
}

另一种变体,使用 Remote 推送到原点:

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);
}