How can I pull a remote repository with GitPython? How can I pull a remote repository with GitPython? python python

How can I pull a remote repository with GitPython?


I managed this by getting the repo name directly:

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


Hope you are looking for this :

import gitg = 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 1stderr: 'ssh: Could not resolve hostname github.com: Name or service not knownfatal: Could not read from remote repository.Please make sure you have the correct access rightsand the repository exists.'