libgit2sharp remove remote branch

空扰寡人 提交于 2019-12-01 23:31:24

问题


I want to delete a branch locally and remote. My code:

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]);
}

But I get the 401 error ("Unauthorized"). And it's because of the presence of the ":" in the name of the branch.

But I read that they are necessary, because of they are like "--delete" in native git.

Thanks for any helps!


回答1:


This is failing with a 401 Unauthorized error because it is unauthorized. To fix this error, you just need to pass the options containing your credentials to the Push() method:

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

That fixed this problem for me.




回答2:


I just figured out the solution from the libgit2 sourcecode.

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

The part +:/refs/heads/to-remove-branch of the refspec specifies that the remove command is forced, else just use :/refs/heads/to-remove-branch

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



来源:https://stackoverflow.com/questions/34307963/libgit2sharp-remove-remote-branch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!