Checkout or list remote branches in GitPython

后端 未结 6 871
轮回少年
轮回少年 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 21:59

    To list branches currently you can use:

    from git import Repo
    r = Repo(your_repo_path)
    repo_heads = r.heads # or it's alias: r.branches
    

    r.heads returns git.util.IterableList (inherits after list) of git.Head objects, so you can:

    repo_heads_names = [h.name for h in repo_heads]
    

    And to checkout eg. master:

    repo_heads['master'].checkout() 
    # you can get elements of IterableList through it_list['branch_name'] 
    # or it_list.branch_name
    

    Module mentioned in the question is GitPython which moved from gitorious to Github.

提交回复
热议问题