这是关于在没有本地工作目录的情况下推送到远程存储库的问题的后续问题(Python 将文件推送到 Github 远程仓库,无需本地工作目录 https://stackoverflow.com/questions/39737192/python-push-files-to-github-remote-repo-without-local-working-directory/39770084?noredirect=1#comment67866611_39770084)。我想知道如果该文件已存在于远程存储库中并且我只想使用同名的修改文件来更新它怎么办? (例如,相当于在 Github 网站上,上传远程上已存在的文件的修改版本)
编辑:我们想出了一个解决方案:
contents_object = repository.contents(file_path)
push_status = contents_object.update("test_message",contents)
然而,虽然这在一台机器上成功运行,但它在另一台机器上引发了错误(具体来说,第一行将收到 AttributeError)。这是因为 github3 的版本可能不同吗?
很明显,在 github3 版本 0.9.6 下,到目前为止您将获得pip install github3.py
(https://github3py.readthedocs.io/en/master/#installation https://github3py.readthedocs.io/en/master/#installation),这将起作用(在没有任何本地工作目录的情况下更新远程存储库):
def update_to_git(username,password,path,account,repo,message):
files_to_upload = [path]
gh = github3.login(username=username, password=password)
repository = gh.repository(account, repo)
for file_info in files_to_upload:
with open(file_info, 'rb') as fd:
contents = fd.read()
contents_object = repository.contents(file_info)
contents_object.update(message,contents)
但是,如果你有 github3 版本 1.0.0a4,这将不起作用。具体来说,您将得到一个AttributeError
为了contents_object = repository.contents(file_info)
行,可能是由于 github3 中的实现发生了变化。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)