我創建了github.com新的存儲庫,然後將其與如何將新文件推送到GitHub?
git clone https://github.com/usrname/mathematics.git
克隆到我的本地機器我增加3個新的文件的文件夾下,現在mathematics
$ tree
.
├── LICENSE
├── numerical_analysis
│ └── regression_analysis
│ ├── simple_regression_analysis.md
│ ├── simple_regression_analysis.png
│ └── simple_regression_analysis.py
,我想上傳3個新文件到我的GitHub使用Python,更具體地說,PyGithub。這是我曾嘗試:
#!/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()
我不知道
- 如何得到字符串
sha
爲repo.get_git_tree
- 我怎麼做的步驟2和3之間,即連接推具體文件
個人而言,PyGithub documentation是不可讀的。尋找很長時間後,我無法找到正確的api。
爲了得到'沙'你需要使用'hashlib' –
@WayneWerner這絕對*不*他應該做什麼。 'sha'是由'git'計算的,如果你自己計算一下,你肯定會錯誤的。 –
@BrianMalehorn我有一個bash腳本,可以通過CURL和github API上傳我的git提交,IIRC - 它不是*那*不好。 –