【问题标题】:How to use libgit2sharp to create a new branch from local to remote?如何使用 libgit2sharp 创建从本地到远程的新分支?
【发布时间】:2014-04-10 07:52:40
【问题描述】:

我想使用 libgit2sharp 在 git 上创建和删除一个分支。我想出了这段代码,但它在repo.Network.Push(localBranch, pushOptions); 处引发错误

using (var repo = new Repository(GIT_PATH))
{
    var branch = repo.CreateBranch(branchName);

    var localBranch = repo.Branches[branchName];

    //repo.Index.Stage(GIT_PATH);
    repo.Checkout(localBranch);
    repo.Commit("Commiting at " + DateTime.Now);

    var pushOptions = new PushOptions() { Credentials = credentials };

    repo.Network.Push(localBranch, pushOptions); // error

    branch = repo.Branches["origin/master"];
    repo.Network.Push(branch, pushOptions);
}

错误信息是The branch 'buggy-3' ("refs/heads/buggy-3") that you are trying to push does not track an upstream branch.

我尝试在互联网上搜索此错误,但没有找到可以解决此问题的解决方案。是否可以使用 libgit2sharp 做到这一点?

【问题讨论】:

    标签: c# git libgit2sharp


    【解决方案1】:

    您必须将本地分支与要推送的远程关联。

    例如,给定一个已经存在的"origin" 远程:

    Remote remote = repo.Network.Remotes["origin"];
    
    // The local branch "buggy-3" will track a branch also named "buggy-3"
    // in the repository pointed at by "origin"
    
    repo.Branches.Update(localBranch,
        b => b.Remote = remote.Name,
        b => b.UpstreamBranch = localBranch.CanonicalName);
    
    // Thus Push will know where to push this branch (eg. the remote)
    // and which branch it should target in the target repository
    
    repo.Network.Push(localBranch, pushOptions);
    
    // Do some stuff
    ....
    
    // One can call Push() again without having to configure the branch
    // as everything has already been persisted in the repository config file
    repo.Network.Push(localBranch, pushOptions);
    

    注意: Push() 公开了其他 overloads,允许您动态提供此类信息,而无需将其存储在配置中。

    【讨论】:

    • 另请参阅此 SO answer,它应该为您提供有关分支配置的更多详细信息
    • localReporepo的区别在哪里?
    猜你喜欢
    • 1970-01-01
    • 2015-03-30
    • 2014-08-09
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多