python-git 在 repo 中签出文件

was*_*256 4 python git

我正在尝试使用 python-git 从远程更新存储库中的单个文件(签出)。当使用命令行执行此操作时,可能会使用git checkout <file>,但我还没有找到在 python-git 中执行此操作的正确方法?到目前为止,我只能对整个存储库进行拉取,我想将其限制为仅一个文件

from git import Repo

repo = Repo('./')
origin = repo.remote()
ret = origin.pull()
Run Code Online (Sandbox Code Playgroud)

AlE*_*ich 5

我已经能够对文档的这一部分做你想做的事情:直接使用 git,这表明某些特定操作可能不会被包装。这是我实现远程执行您想要的操作的唯一方法:

from git import Repo
repo = Repo('./')
origin = repo.remote()

cli = origin.repo.git
cli.checkout('origin/master', 'path/to/file')
Run Code Online (Sandbox Code Playgroud)