10

I need to delete a remote branch, and found this technique:

git push origin :the_remote_branch

I tried passing it to Networks Push method in the following forms, but nothing seems to work (options is my login credentials):

_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)

And a few other options. I cannot get it to work at all, it returns errors such as (for the ":NewBranchForDeletion" method):

Not a valid reference "NewBranchForDeletion"

Update:

Thanks to @Rob for finding me this comment on LibGit2Sharp's repo: https://github.com/libgit2/libgit2sharp/issues/466#issuecomment-21076975

The first option fails with a NullReferenceException on objectish, and using string.Empty for objectish results in the error mentioned above. The second option is what I am trying, except I am using the version with HTTPS validation:

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");
Community
  • 1
  • 1
  • I don't know this library, but why are you calling `Push` twice per line ? It seems to me like the solution would be along the lines of `Push(_repo.Network.Remotes["Origin"], "", "NewBranchForDeletion", options)` – Rob Mar 03 '16 at 05:16
  • Whoops, copy/pasta error. –  Mar 03 '16 at 05:17
  • Alternatively, do you have a working solution for a non-deleting push? Something that mimics `git push origin dev:master` ? – Rob Mar 03 '16 at 05:17
  • 1
    Yes. `_repo.Network.Push(branch, options);` Hmm, I'll try that. –  Mar 03 '16 at 05:18
  • This seems to have the solution: https://github.com/libgit2/libgit2sharp/issues/466#issuecomment-21076975 – Rob Mar 03 '16 at 05:21
  • @Rob I appreciate your help, but that has exactly the same error. In fact, the second option is what I was currently trying. I will update my question with this. –  Mar 03 '16 at 05:41
  • Hmm, in that case it seems like it's a bug - might have to lodge an issue with the maintainers (since the solution at least used to work, it appears) – Rob Mar 03 '16 at 05:59
  • The maintainers watch the tag, so they should see this soon. –  Mar 03 '16 at 06:10
  • There doesn't seem to be a test in the repo for it yet. =( https://github.com/libgit2/libgit2sharp/blob/vNext/LibGit2Sharp.Tests/PushFixture.cs – RubberDuck Mar 03 '16 at 09:27
  • 1
    Have you considered the `public virtual void Push(Remote remote, string pushRefSpec)` overload? Passing a `:refs/heads/branch_to_delete` refspec should work. – nulltoken Mar 03 '16 at 11:18
  • @nulltoken That worked! I had tried `refs/heads/:branch_to_delete` before, which didn't work, I didn't think of that option. Please post that as the answer. –  Mar 03 '16 at 17:39

1 Answers1

5

As stated in the documentation, a 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>."

It also mentions that "Pushing an empty <src> allows you to delete the <dst> ref from the remote repository."

Considering these above, using the void Push(Remote remote, string pushRefSpec) overload, and passing :refs/heads/branch_to_delete as the pushRefSpec should do the trick.

nulltoken
  • 64,429
  • 20
  • 138
  • 130