How can I pull a remote repository with GitPython?

余生颓废 提交于 2019-12-18 14:47:54

问题


I am trying to find the way to pull a git repository using gitPython. So far this is what I have taken from the official docs here.

test_remote = repo.create_remote('test', 'git@server:repo.git')
repo.delete_remote(test_remote) # create and delete remotes
origin = repo.remotes.origin    # get default remote by name
origin.refs                     # local remote references
o = origin.rename('new_origin') # rename remotes
o.fetch()                       # fetch, pull and push from and to the remote
o.pull()
o.push()

The fact is that I want to access the repo.remotes.origin to do a pull withouth renaming it's origin (origin.rename) How can I achieve this? Thanks.


回答1:


I managed this by getting the repo name directly:

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



回答2:


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.'



回答3:


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.



来源:https://stackoverflow.com/questions/13166595/how-can-i-pull-a-remote-repository-with-gitpython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!