如何在NGit中使用Ls-Remote



我希望使用NGit执行以下操作,但是将近一整天后完全迷失了:

  • 创建空存储库
  • 使用 URL 和凭据添加远程"源"
  • 运行 Ls-Remote 以获取master分支的最新哈希值origin

如果有人能给我看一个例子,我将不胜感激

using System.Collections.Generic;
using System.Linq;
using NGit.Api;
using Sharpen;
// git init
string path = @"C:gitrepo1";
Git git = Git.Init().SetDirectory(new FilePath(path)).Call();
// after init, you can call the below line to open
// Git git = Git.Open(new FilePath(path));
// git remote origin
StoredConfig config = git.GetRepository().GetConfig();
config.SetString("remote", "origin", "url", @"http://user:password@github.com/user/repo1.git");
config.Save();
// git ls-remote
ICollection<Ref> refs = git.LsRemote().SetRemote("origin").Call();
Ref master = refs.FirstOrDefault(a => a.GetName() == "refs/heads/master");
if (master != null)
{
    string hash = master.GetObjectId().Name;
}

最新更新