Checkout or list remote branches in GitPython

后端 未结 6 873
轮回少年
轮回少年 2020-12-18 21:29

I don\'t see an option to checkout or list remote/local branches in this module: https://gitpython.readthedocs.io/en/stable/

6条回答
  •  心在旅途
    2020-12-18 22:05

    I had a similar issue. In my case I only wanted to list the remote branches that are tracked locally. This worked for me:

    import git
    
    repo = git.Repo(repo_path)
    branches = []
    for r in repo.branches:
        branches.append(r)
        # check if a tracking branch exists
        tb = t.tracking_branch()
        if tb:
            branches.append(tb) 
    

    In case all remote branches are needed, I would prefer running git directly:

    def get_all_branches(path):
        cmd = ['git', '-C', path, 'branch', '-a']
        out = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
        return out
    

提交回复
热议问题