删除远程分支

10

我需要删除一个远程分支,并找到了以下操作:

git push origin :the_remote_branch

我尝试将其以以下形式传递给Networks Push方法,但似乎没有任何作用(options是我的登录凭据):

_repo.Network.Push(_repo.Network.Remotes["origin"], "origin/:NewBranchForDeletion", options)  
_repo.Network.Push(_repo.Network.Remotes["origin"], ":NewBranchForDeletion", options)  
_repo.Network.Push(_repo.Network.Remotes["origin"], ":origin/NewBranchForDeletion", options)
_repo.Network.Push(_repo.Network.Remotes["origin"], ":refs/remotes/:origin/NewBranchForDeletion", options)
_repo.Network.Push(_repo.Network.Remotes["origin"], ":refs/remotes/origin/NewBranchForDeletion", options)
_repo.Network.Push(_repo.Network.Remotes["origin"], "refs/heads/:origin/NewBranchForDeletion", options)
_repo.Network.Push(_repo.Network.Remotes["origin"], "refs/heads/:NewBranchForDeletion", options)

还有其他几个选项。我完全弄不好,它会返回错误,例如(对于“:NewBranchForDeletion”方法):

“NewBranchForDeletion”无效的引用

更新:

感谢@Rob在LibGit2Sharp的存储库中找到了这条评论:https://github.com/libgit2/libgit2sharp/issues/466#issuecomment-21076975

第一个选项在“objectish”上失败并引发NullReferenceException,在objectish上使用string.Empty会导致上述错误。我正在尝试第二个选项,但我正在使用带有HTTPS验证的版本:

repo.Network.Push(repo.Remotes["my-remote"], objectish: null, destinationSpec: "my-branch");

// Or using a refspec, like you would use with git push...
repo.Network.Push(repo.Remotes["my-remote"], pushRefSpec: ":my-branch");

我不熟悉这个库,但是你为什么要在同一行调用 Push 两次呢?在我看来,解决方案应该类似于 Push(_repo.Network.Remotes["Origin"], "", "NewBranchForDeletion", options) - Rob
1
是的。_repo.Network.Push(branch, options); 嗯,我会试试看。 - user2509848
@Rob,感谢你的帮助,但是还是出现了同样的错误。事实上,第二个选项就是我目前正在尝试的。我会在我的问题中更新这一点。 - user2509848
1
你考虑过使用 public virtual void Push(Remote remote, string pushRefSpec) 重载吗?传递一个 :refs/heads/branch_to_delete refspec 应该可以工作。 - nulltoken
@nulltoken,太好了!我之前尝试过refs/heads/:branch_to_delete,但没有成功,我没想到还有这个选项。请把它作为答案发布。 - user2509848
显示剩余6条评论
1个回答

5
根据文档所述,refspec "规定了使用哪个源对象来更新哪个目标引用。 参数的格式是一个可选加号加上源对象,然后是冒号<:>,最后是目标引用。"
它还提到,“推送空允许您从远程存储库中删除引用。”
考虑到以上内容,使用void Push(Remote remote,string pushRefSpec)重载,并将:refs / heads / branch_to_delete作为pushRefSpec传递即可。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接