首页 > 解决方案 > 如何结帐分行

问题描述

我让 gitpython 工作来克隆一个 repo,现在我需要检查该 repo 中的分支我一直在查看文档但似乎找不到它

import git
import os
import shutil

DIR_NAME = "temp"
REMOTE_URL = "<REPO>"

if os.path.isdir(DIR_NAME):
    shutil.rmtree(DIR_NAME)

os.mkdir(DIR_NAME)

repo = git.Repo.init(DIR_NAME)
origin = repo.create_remote('origin', REMOTE_URL)
origin.fetch()
origin.pull(origin.refs[0].remote_head)

标签: pythongitpython

解决方案


repo.git.checkout('branchename')

或者,如果分支尚不存在..

repo.git.checkout('-b', 'branchename')

推荐阅读