2012-10-31 53 views
8

我想找到使用gitPython来拉取git仓库的方法。 到目前为止,这是我从官方文档here中取得的。如何使用GitPython来获取远程存储库?

test_remote = repo.create_remote('test', '[email protected]: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做一个拉withouth的重新命名它的原点(origin.rename) 我怎样才能做到这一点? 谢谢。

回答

15

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

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

这里的'repo_name'实际上不是repo的名称,而是git仓库基础的文件系统路径。 –

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 

在另一方面using git commands with GitPythonrepo.git.pull()显示真正的错误:

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

希望你正在寻找为此:

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

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

相关问题