2012-10-31 56 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') 

爲給定的存儲庫和分支提取最新的提交。

相關問題