libgit2sharp删除远程分支

时间:2015-12-16 09:14:18

标签: c# git libgit2sharp

我想在本地和远程删除分支。 我的代码:

using (var repository = new Repository(path))
{
    var remote = repository.Network.Remotes["origin"];
    var options = new PushOptions();
    var credentials = options.CredentialsProvider = GetUserCredentialsProvider();
    options.CredentialsProvider = credentials;
    string pushRefSpec = @"refs/heads/:{0}".FormatWith(branch);
    repository.Network.Push(remote, pushRefSpec);
    repository.Branches.Remove(repository.Branches[branch]);
}

但我得到401错误(“未经授权”)。 这是因为分支名称中存在“:”。

但我读到它们是必要的,因为它们在本机git中就像“--delete”。

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

由于未经授权,因此未通过401 Unauthorized错误。要解决此错误,您只需将包含您的凭据的options传递给Push()方法:

repository.Network.Push(remote, pushRefSpec, options)

这解决了这个问题。

答案 1 :(得分:0)

我只是从libgit2源代码中找出解决方案。

repository.Network.Push(origin, "+:/refs/heads/to-remove-branch")

refspec的+:/refs/heads/to-remove-branch部分指定强制执行remove命令,否则只需使用:/refs/heads/to-remove-branch

来源:https://github.com/libgit2/libgit2/blob/master/tests/online/push.c

相关问题