How can I pull a remote repository with GitPython?

Hope you are looking for this :

import git
g = git.Git('git-repo')
g.pull('origin','branch-name')

Pulls latest commits for the given repository and branch.


As the accepted answer says it's possible to use repo.remotes.origin.pull(), but the drawback is that it hides the real error messages into it's own generic errors. For example when DNS resolution doesn't work, then repo.remotes.origin.pull() shows the following error message:

git.exc.GitCommandError: 'Error when fetching: fatal: Could not read from remote repository.
' returned with exit code 2

On the other hand using git commands with GitPython like repo.git.pull() shows the real error:

git.exc.GitCommandError: 'git pull' returned with exit code 1
stderr: 'ssh: Could not resolve hostname github.com: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.'

I managed this by getting the repo name directly:

 repo = git.Repo('repo_path')
 o = repo.remotes.origin
 o.pull()