从远程仓库获取分支名称

get branch name from remote repo

我尝试从远程仓库获取分支

            var credentials = new UsernamePasswordCredentials
            {
                Username = "username",
                Password = "pass"
            };

            string path = @"https://github.com/torvalds/linux.git";
            var repo = Repository.ListRemoteReferences(path, (url, fromUrl, types) => credentials);


            foreach (var reference in repo)
            {
                Console.WriteLine(reference.TargetIdentifier);
            }

如果我使用 reference.CanonicalName 首先是 HEAD 其他是提交

如果我使用 reference.TargetIdentifier 我有完整的名字 refs/heads/master (我也只需要最后一部分),其他是提交哈希

我怎么只能得到分支名称?

TargetIdentifier,您应该能够提取分支名称:

string branchName = reference.TargetIdentifier.Replace('refs/heads', ''); 

对于所有分支,如 this project:

branches = Repository.ListRemoteReferences("https://github.com/shadow999999/Translators-SOL")
  .Where(elem => elem.IsLocalBranch)
  .Select(elem => elem.CanonicalName
  .Replace("refs/heads/", ""));