How to get the current checked out Git branch name through pygit2?

This question should be related to:

  • How to get the current branch name in Git?
  • Get git current branch/tag name
  • How to get the name of the current git branch into a variable in a shell script?
  • How to programmatically determine the current checked out Git branch
  • But I am wondering how to do that through pygit2?


    From PyGit Documentation

    Either of these should work

    head = repo.lookup_reference('HEAD').resolve()
    head = repo.head
    
    branch_name = head.name
    

    要获得传统的“速记”名称:

    from pygit2 import Repository
    
    Repository('.').head.shorthand  # 'master'
    
    链接地址: http://www.djcxy.com/p/19502.html

    上一篇: git仅为当前分支提取

    下一篇: 如何通过pygit2获取当前签出的Git分支名称?