首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用GitHub和PyGit2创建、克隆和推送到PyGitHub回购

使用GitHub和PyGit2创建、克隆和推送到PyGitHub回购
EN

Stack Overflow用户
提问于 2018-03-23 21:09:19
回答 1查看 8K关注 0票数 11

我如何创建一个新的GitHub存储库,克隆它,更改文件,然后使用python以及pyGitHub和pyGit2库将它推回github?

这两个库的文档非常稀少,几乎没有示例。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-23 21:18:25

我就是这么做的。我并不是说这是实现这一目标的绝对最佳方式,但我希望它能为将来的某些人树立一个良好的榜样。

代码语言:javascript
复制
from github import Github
import pygit2

# using username and password establish connection to github
g = Github(userName, password)
org = g.get_organization('yourOrgName')

#create the new repository
repo = org.create_repo(projectName, description = projectDescription )

#create some new files in the repo
repo.create_file("/README.md", "init commit", readmeText)

#Clone the newly created repo
repoClone = pygit2.clone_repository(repo.git_url, '/path/to/clone/to')

#put the files in the repository here

#Commit it
repoClone.remotes.set_url("origin", repo.clone_url)
index = repoClone.index
index.add_all()
index.write()
author = pygit2.Signature("your name", "your email")
commiter = pygit2.Signature("your name", "your email")
tree = index.write_tree()
oid = repoClone.create_commit('refs/heads/master', author, commiter, "init commit",tree,[repoClone.head.get_object().hex])
remote = repoClone.remotes["origin"]
credentials = pygit2.UserPass(userName, password)
remote.credentials = credentials

callbacks=pygit2.RemoteCallbacks(credentials=credentials)

remote.push(['refs/heads/master'],callbacks=callbacks)

我花了两天的时间试图解决这个问题的例子不足的问题,所以我希望这对将来的人有所帮助。

票数 32
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49458329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档