pythongitgitpython

Checkout or list remote branches in GitPython


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


Solution

  • After you’ve done

    from git import Git
    g = Git()
    

    (and possibly some other command to init g to the repository you care about) all attribute requests on g are more or less transformed into a call of git attr *args.

    Therefore:

    g.checkout("mybranch")
    

    should do what you want.

    g.branch()
    

    will list the branches. However, note that these are very low level commands and they will return the exact code that the git executables will return. Therefore, don’t expect a nice list. I’ll just be a string of several lines and with one line having an asterisk as the first character.

    There might be some better way to do this in the library. In repo.py for example is a special active_branch command. You’ll have to go through the source a little and look for yourself.