I created a new repository on github.com and then cloned it to my local machine with
git clone https://github.com/usrname/mathematics.git
I added 3 new files under the folder mathematics
$ tree
.
├── LICENSE
├── numerical_analysis
│ └── regression_analysis
│ ├── simple_regression_analysis.md
│ ├── simple_regression_analysis.png
│ └── simple_regression_analysis.py
Now, I'd like to upload 3 new files to my GitHub using Python, more specifically, PyGithub. Here is what I have tried:
#!/usr/bin/env python
# *-* coding: utf-8 *-*
from github import Github
def main():
# Step 1: Create a Github instance:
g = Github("usrname", "passwd")
repo = g.get_user().get_repo('mathematics')
# Step 2: Prepare files to upload to GitHub
files = ['mathematics/numerical_analysis/regression_analysis/simple_regression_analysis.py', 'mathematics/numerical_analysis/regression_analysis/simple_regression_analysis.png']
# Step 3: Make a commit and push
commit_message = 'Add simple regression analysis'
tree = repo.get_git_tree(sha)
repo.create_git_commit(commit_message, tree, [])
repo.push()
if __name__ == '__main__':
main()
I don't know
- how to get the string
shaforrepo.get_git_tree - how do I make a connection between step 2 and 3, i.e. pushing specific files
Personally, PyGithub documentation is not readable. I am unable to find the right api after searching for long time.
shayou'll need to usehashlib- Wayne Wernershais computed bygitand you'll almost certainly get it wrong if you try to compute it yourself. - Brian Malehorngitdirectly to interface with it? Or using a python interface such as GitPython, not necessarily GitHub oriented? Its documentation is indeed... very sparse, and I would not call it usable. - MayeulCshavalues for the commit, and the base tree). with that in hand, callcreate_git_treepassing the HEAD's tree as base, and giving it a list ofInputGitTreeElement(settingcontent, but leavingshaalone) with your modifications. then callcreate_git_commitwith your new tree. Finally, you'll need to get the branch ref and update it to your new commitsha. It's probably easier to find some library that wraps this one, than do it yourself though. - Hasturkun