我在github.com上创建了一个新的存储库,然后将其克隆到我的本地机器上
git clone https://github.com/usrname/mathematics.git
Run Code Online (Sandbox Code Playgroud)
我在该文件夹下添加了3个新文件 mathematics
$ tree
.
??? LICENSE
??? numerical_analysis
? ??? regression_analysis
? ??? simple_regression_analysis.md
? ??? simple_regression_analysis.png
? ??? simple_regression_analysis.py
Run Code Online (Sandbox Code Playgroud)
现在,我想使用Python,更具体地说,PyGithub将3个新文件上传到我的GitHub .这是我尝试过的:
#!/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 …Run Code Online (Sandbox Code Playgroud) 以下是我认为应该可行的三种方法,因此按顺序尝试:
使用pygithub : (Github 的 python API) 将推送请求发送到我的存储库。失败,因为我在 API 中找不到推送功能。我可以看到编辑文件,但是当我计划经常替换文件时,这无济于事。
git push在 python 子进程 (HTTPS) 的命令行中使用:这几乎有效,但我不知道如何填写所需的用户和密码字段。试图:
import subprocess
from pexpect import popen_spawn
user = 'GithubUsername'
password = '***********'
cmd = "cd C:\\Users\Dropbox\git-test"
returned_value = subprocess.call(cmd, shell=True) # returns the exit code in unix
cmd = "git add ."
subprocess.call(cmd, shell=True)
cmd = 'git commit -m "python project update"'
subprocess.call(cmd, shell=True)
cmd = "git remote set-url origin https://github.com/Tehsurfer/git-test.git"
subprocess.call(cmd, shell=True)
cmd = "git push …Run Code Online (Sandbox Code Playgroud)