首页 > 解决方案 > 如何从 github 存储库中获取分支列表?

问题描述

我想要一个来自 github 存储库的所有分支的列表。所以我可以做这样的事情。

print(branches[1])

并且会得到分支名称的输出。我还没有找到关于如何做到这一点的直接答案。

标签: pythongithub

解决方案


import subprocess

subprocess.check_output(['git', 'branch']).decode().split('\n')

另一种可能:

from pygit2 import Repository

repo = Repository(path_to_your_repo) # use '.' if you are already there
repo.branches

推荐阅读