简体   繁体   English

如何使用 LibGit2Sharp 将本地分支推送到远程存储库

[英]How to push a local branch to a remote repository using LibGit2Sharp

Trying to create a new branch and push it to my remote repo using this code:尝试使用以下代码创建一个新分支并将其推送到我的远程仓库:

        var localBranch = repository.CreateBranch(environment);
        Commands.Checkout(repository, localBranch);
        var remote = repository.Network.Remotes.First();
        repository.Branches.Update(localBranch, b => b.Remote = remote.Name, b => b.UpstreamBranch = localBranch.CanonicalName);
        repository.Network.Push(localBranch, pushOptions);

Unfortunately I am getting the following exception:不幸的是,我收到以下异常:

   LibGit2Sharp.BareRepositoryException: local push doesn't (yet) support pushing to non-bare repos.
   at LibGit2Sharp.Core.Ensure.HandleError(Int32 result)
   at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result)
   at LibGit2Sharp.Core.Proxy.git_remote_push(RemoteHandle remote, IEnumerable`1 refSpecs, GitPushOptions opts)
   at LibGit2Sharp.Network.Push(Remote remote, IEnumerable`1 pushRefSpecs, PushOptions pushOptions)
   at LibGit2Sharp.Network.Push(Remote remote, String pushRefSpec, PushOptions pushOptions)
   at LibGit2Sharp.Network.Push(IEnumerable`1 branches, PushOptions pushOptions)
   at LibGit2Sharp.Network.Push(Branch branch, PushOptions pushOptions)

What am I missing?我错过了什么? isn't it possible to push a new branch to a remote repository?是否可以将新分支推送到远程存储库?

So after changing my flow to the following it is now working an I am not getting the error that I got before.因此,在将我的流程更改为以下内容后,它现在可以正常工作了,但我没有收到之前遇到的错误。

    private static void CreateRemoteBranch(Repository repository, string environment, string master, PushOptions pushOptions)
    {
        Logger.Log($"Checking to see if a remote environment branch for {environment} exists");
        
        var masterBranch = repository.Branches.RemoteBranch(master);
        var remoteBranch = repository.Branches.RemoteBranch(environment);
        
        if (remoteBranch == null)
        {
            Logger.Log("Creating the new branch");
            var localBranch = repository.CreateBranch(environment, masterBranch.Tip);
            
            Logger.Log("Pushing it to remote");
            var remote = repository.Network.Remotes.First();
            repository.Branches.Update(localBranch, b => b.Remote = remote.Name, b => b.UpstreamBranch = localBranch.CanonicalName);
            repository.Network.Push(localBranch, pushOptions);
        }
        else
        {
            Logger.Log("Branch exists");
        }
    }


public static class BranchUtils
{
    public static Branch RemoteBranch(this BranchCollection branchCollection, string name)
    {
        return branchCollection[$"origin/{name}"];
    }

    public static Branch LocalBranch(this BranchCollection branchCollection, string name)
    {
        return branchCollection[name];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM