我正在尝试将GitHub存储库克隆到现有的非空目录中。我尝试使用git命令行模仿它完成的方式:
git init
git remote add origin https://github.com/[...].git
git fetch
git reset --hard origin/branch
var git = Git.Init().SetDirectory(Location).Call();
Repository = git.GetRepository();
var config = Repository.GetConfig();
config.SetString("remote", "origin", "url", "https://github.com/[...].git");
config.Save();
git.Fetch().Call();
git.Reset().SetRef("origin/branch")
.SetMode(ResetCommand.ResetType.HARD).Call();
在这种特殊情况下,我收到了“无法获取”错误。我尝试了很多不同的东西,包括使用BranchCreate克隆到临时字典中......但我总是遇到一个问题某个地方。
那么您将如何正确克隆存储库并将其设置为以后获取更新?
答案 0 :(得分:1)
虽然克隆比git init .
+ git remote add origin ...
+ git fetch
+ git reset --hard origin/master
更容易,但确实非空文件夹需要该序列。
在这种情况下,你需要告诉Git要获取什么,如OP所评论的那样:
git.Fetch().SetRefSpecs(new RefSpec("+refs/heads/*:refs/remotes/origin/*")).Call();
这将允许git.Fetch().Call();
实际获取内容。
(这就是NGit.Test/NGit.Api/FetchCommandTest.cs
L61-L82正在做的事情)
在extensive discussion in the chat之后,OP正在使用here is the code:
var cloneUrl = ...;
var branchName = ...;
var git = Git.Init().SetDirectory(Location).Call();
Repository = git.GetRepository();
// Original code in question works, is shorter,
// but this is most likely the "proper" way to do it.
var config = Repository.GetConfig();
RemoteConfig remoteConfig = new RemoteConfig(config, "origin");
remoteConfig.AddURI(new URIish(cloneUrl));
// May use * instead of branch name to fetch all branches.
// Same as config.SetString("remote", "origin", "fetch", ...);
remoteConfig.AddFetchRefSpec(new RefSpec(
"+refs/heads/" + Settings.Branch +
":refs/remotes/origin/" + Settings.Branch));
remoteConfig.Update(config);
config.Save();
git.Fetch().Call();
git.BranchCreate().SetName(branchName).SetStartPoint("origin/" + branchName)
.SetUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).Call();
git.Checkout().SetName(branchName).Call();
// To update the branch:
git.Fetch().Call();
git.Reset().SetRef("origin/" + branchName).Call();