GitPython: How to check out a Branch

If you check a branch using Python code, the first thing you need to do is make sure you have gitpython module installed.

pip3 install gitpython

Now let's check out a git project,

import git

git_repo = 'repos/code2care/CustomRatingBarExample'

branch_name = 'master'
repo = git.Repo(git_repo)

try:
    repo.git.checkout(branch_name)
    print(f"Checked out branch '{branch_name}'.")
except git.exc.GitCommandError as e:
    print(f"Error occurred while checking out branch '{branch_name}': {e}")
Checkout Git Project using GitPython Example

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!