python-git checkout file in repo

Viewed 3032

I'm trying to use python-git to update a single file (checkout) in the repository from remote. When doing this with command line one would probably use git checkout <file> but I haven't found a proper way of doing this in python-git? So far I was only able to do a pull on the entire repo, what I want to limit this to one file only

from git import Repo

repo = Repo('./')
origin = repo.remote()
ret = origin.pull()
2 Answers

I have been able to do what you want with that part of documentation : Using git directly, which tells that some specific actions may not be wrapped. This is the only way I achieve to do what you want from remote:

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

cli = origin.repo.git
cli.checkout('origin/master', 'path/to/file')

From API, you can use Checkout:

You can indicate the file location as arg and version to checkout in kwargs

Related