Ad

Get Branch Name From Remote Repo

- 1 answer

i try to get branches 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);
            }

if i use reference.CanonicalName first is HEAD and other is commits

if i use reference.TargetIdentifier i have full first name refs/heads/master (also i need only last part) and other is commits hashes

how i can get only branch names?

Ad

Answer

From the TargetIdentifier, you should be able to extract the branch name with:

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

For all branches, as in this project:

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