以下是我认为应该可行的三种方法,以便按顺序尝试:
git push
:这几乎可以工作,但我无法弄清楚如何填写所需的用户和密码字段。尝试:
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 "
child_process = popen_spawn.PopenSpawn(cmd)
child_process.expect('User')
child_process.sendline(user)
child_process.expect('Password')
child_process.sendline(password)
print('returned value:', returned_value)
print('end of commands')`
git push
:我遇到的问题是我找不到在Windows命令提示符下创建ssh代理的方法。我已经能够通过this tutorial轻松地在MINGW64终端中创建一个,但无法通过Python与它进行交互。How do I push new files to GitHub?
一个非常相似的问题,我能够修改哪些代码,通过python将多个文件推送到github:
import base64
from github import Github
from github import InputGitTreeElement
user = "GithubUsername"
password = "*********"
g = Github(user,password)
repo = g.get_user().get_repo('git-test')
file_list = [
'C:\\Users\jesse\Dropbox\Swell-Forecast\git-test\index.html',
'C:\\Users\jesse\Dropbox\Swell-Forecast\git-test\margin_table.html'
]
file_names = [
'index.html',
'margin_table.html'
]
commit_message = 'python update 2'
master_ref = repo.get_git_ref('heads/master')
master_sha = master_ref.object.sha
base_tree = repo.get_git_tree(master_sha)
element_list = list()
for i, entry in enumerate(file_list):
with open(entry) as input_file:
data = input_file.read()
if entry.endswith('.png'):
data = base64.b64encode(data)
element = InputGitTreeElement(file_names[i], '100644', 'blob', data)
element_list.append(element)
tree = repo.create_git_tree(element_list, base_tree)
parent = repo.get_git_commit(master_sha)
commit = repo.create_git_commit(commit_message, tree, [parent])
master_ref.edit(commit.sha)