gpt4 book ai didi

c# - 如何使用 libgit2sharp 创建从本地到远程的新分支?

转载 作者:太空狗 更新时间:2023-10-29 12:44:38 25 4
gpt4 key购买 nike

我想使用 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);
}

错误消息是您尝试推送的分支“buggy-3”(“refs/heads/buggy-3”)未跟踪上游分支。

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

最佳答案

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

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

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 允许您动态提供此类信息,而无需将其存储在配置中。

关于c# - 如何使用 libgit2sharp 创建从本地到远程的新分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22981934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com