我想使用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.
我尝试在Internet上搜索此错误,但找不到能解决该问题的解决方案。是否可以使用libgit2sharp做到这一点?
您必须将本地分支与要推送的远程对象相关联。
例如,给定一个已经存在的"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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句