How to get the current/active branch with LibGit2Sharp?

百般思念 提交于 2020-06-25 07:32:11

问题


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?


回答1:


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...




回答2:


I think that, instead of going through the branches and checking whether each branch is the current head, the simplest approach is to directly get the branch name from the repository Head:

using (var repo = new Repository(@"path to .git"))
{
    var currentBranchName = repo.Head.FriendlyName;
}

You can then obtain the branch itself via

repo.Branches[currentBranchName]


来源:https://stackoverflow.com/questions/12837311/how-to-get-the-current-active-branch-with-libgit2sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!