Bar*_*ith 3 python pygit2 pygithub
如何创建一个新的GitHub存储库,对其进行克隆,更改文件,然后使用python和pyGitHub和pyGit2库将其推回github?
这两个库的文档都很稀疏,几乎没有示例。
Bar*_*ith 10
这就是我使它起作用的方式。我并不是要指出这是实现此目标的绝对最佳方法,但我希望它可以为将来的某个人提供一个很好的例子。
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)
Run Code Online (Sandbox Code Playgroud)
我花了两天的时间尝试解决这些问题,但我希望这对以后的工作有所帮助。