删除远程分支

Delete Remote Branch

我需要删除远程分支,发现了这个技巧:

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)

还有一些其他选项。我根本无法让它工作,它 returns 错误,例如(对于“:NewBranchForDeletion”方法):

Not a valid reference "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");

documentation 中所述,refspec "Specif(ies) what destination ref to update with what source object. The format of a <refspec> parameter is an optional plus +, followed by the source object <src>, followed by a colon :, followed by the destination ref <dst>."

它还提到 "Pushing an empty <src> allows you to delete the <dst> ref from the remote repository."

考虑到以上这些,使用 void Push(Remote remote, string pushRefSpec) 重载,并将 :refs/heads/branch_to_delete 作为 pushRefSpec 传递应该可以解决问题。