Uui*_*uid 14 python git gitpython
我试图找到使用gitPython拉出git存储库的方法.到目前为止,这是我从这里的官方文档中获取的内容.
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()
Run Code Online (Sandbox Code Playgroud)
事实是,我想访问repo.remotes.origin做一个拉动而不重命名它的起源(origin.rename)我怎样才能实现这个目标?谢谢.
Uui*_*uid 29
我通过直接获取回购名称来管理这个:
repo = git.Repo('repo_name')
o = repo.remotes.origin
o.pull()
Run Code Online (Sandbox Code Playgroud)
小智 9
希望你正在寻找这个:
import git
g = git.Git('git-repo')
g.pull('origin','branch-name')
Run Code Online (Sandbox Code Playgroud)
拉取给定存储库和分支的最新提交。
正如接受的答案所说,可以使用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
Run Code Online (Sandbox Code Playgroud)
在另一方面使用Git命令与GitPython像repo.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.'
Run Code Online (Sandbox Code Playgroud)
上面Akhil Singhal的答案中的 git.Git 模块仍然有效,但已重命名为git.cmd.Git,例如:
import git
# pull from remote origin to the current working dir:
git.cmd.Git().pull('https://github.com/User/repo','master')
Run Code Online (Sandbox Code Playgroud)