Search code examples
.netgitlibgit2sharp

How to get the current/active branch with LibGit2Sharp?


So using LibGit2Sharp https://github.com/libgit2/libgit2sharp you can walk through the branches like this

using (var repo = new Repository(@"path to .git"))
{
    foreach (var branch in repo.Branches)
    {
        Debug.WriteLine(branch.Name);   
    }
}

But how do I get the current/active branch?


Solution

  • Branch.IsCurrentRepositoryHead should do the trick.

    I think Repository.Head will also do the same thing if you don't want to iterate through the branches...