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?

  • How to partially stage a file with LibGit2Sharp?
  • Gitflow and testing / deployment
  • How to pack/publish a .NET Standard Library to a private VSTS Nuget feed?
  • C# Cannot read from App.config file in MVC
  • Visual Studio 2008 source control for small teams
  • Using github on my project that uses NuGet
  • git rebase and deleted rebasing branches causing “Interactive rebase already started” error message
  • git remove all deleted files from entire history
  • should I be (git)ignoring my knitr cache?
  • Git pull - Please move or remove them before you can merge
  • Pull remote branch into local repo with different name?
  • Retroactively treat line of Git commits as a branch
  • One Solution collect form web for “How to get the current/active branch with LibGit2Sharp?”

    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…

    Git Baby is a git and github fan, let's start git clone.