如何使用GitPython拉出远程存储库?

时间:2012-10-31 20:01:32

标签: python git gitpython

我正在尝试使用gitPython找到使用git存储库的方法。 到目前为止,这是我从官方文档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()

事实是,我想访问repo.remotes.origin进行拉取而不重命名它的原点(origin.rename) 我怎样才能做到这一点? 感谢。

4 个答案:

答案 0 :(得分:25)

我通过直接获取回购名称来管理这个:

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

答案 1 :(得分:2)

来自Kube DNS的答案Akhil Singhal

git.Git模块仍然有效,但是已经从aboverenamed,例如:

import git 
# pull from remote origin to the current working dir:
git.cmd.Git().pull('https://github.com/User/repo','master')

答案 2 :(得分:0)

正如已接受的答案所说,可以使用repo.remotes.origin.pull(),但缺点是它会将真正的错误消息隐藏到它自己的通用错误中。例如,当DNS解析不起作用时,repo.remotes.origin.pull()会显示以下错误消息:

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

另一方面,像repo.git.pull()这样的using git commands with GitPython显示了真正的错误:

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 :(得分:-2)

希望您正在寻找:

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

提取给定存储库和分支的最新提交。