【问题标题】:libgit2sharp remove remote branchlibgit2sharp 删除远程分支
【发布时间】:2015-12-16 09:14:17
【问题描述】:

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

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”。

感谢您的帮助!

【问题讨论】:

  • 你试过只用repository.Network.Push(remote, ":branchname");吗?
  • 是的。同样的结果...
  • 我正在尝试实现相同的目标,但我总是遇到错误(与身份验证无关),即使答案中发布了内容。你做到了吗?

标签: c# git libgit2sharp


【解决方案1】:

由于未经授权,因此失败并出现 401 Unauthorized 错误。要修复此错误,您只需将包含您的凭据的options 传递给Push() 方法:

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

这为我解决了这个问题。

【讨论】:

    【解决方案2】:

    我刚刚从 libgit2 源代码中找到了解决方案。

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

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

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

    【讨论】:

    • 这给了我一个错误:not a valid reference 'branch-name'。分支存在,所以我不知道为什么它会这样失败......
    • 或者错误invalid refspec +:/refs/heads/branch-name
    • 好的,删除第一个斜线对我来说是:repository.Network.Push(origin, "+:refs/heads/to-remove-branch", pushOptions);
    猜你喜欢
    • 2014-06-13
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    相关资源
    最近更新 更多