如何使用python将本地文件推送到github? (或通过Python发布提交) [英] How to push local files to github using python? (or post a commit via Python)

查看:610
本文介绍了如何使用python将本地文件推送到github? (或通过Python发布提交)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我认为应该可行的三种方法,因此请按顺序尝试:

Here are three methods I thought should be feasible so attempted in order:

  1. 使用 pygithub :(Github的python API)进行操作将推送请求发送到我的存储库.失败,因为我在API中找不到推入功能.我可以看到编辑文件,但是当我计划经常更换文件时,这无济于事.

  1. Use pygithub: (Github's python API) to send push requests to my repository. Failed because I can find no push functions in the API. I can see edit files, but that doesn't help when I plan on replacing the file often.

在python子进程(HTTPS)的命令行中使用git push:这几乎可行,但是我无法弄清楚如何填写所需的用户和密码字段.尝试:

Use git push in command line from a python subprocess (HTTPS): This almost works, but I cannot figure out how to fill in the user and password fields required. Attempt:

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')`

  • 在python子进程(SSH)的命令行中使用git push:我的问题是我找不到在Windows命令提示符下创建ssh代理的方法.我已经能够通过

  • Use git push in command line from a python subprocess (SSH): The problem I had here is that I cannot find a way to create a ssh agent in the windows command prompt. I have been able to create one in the MINGW64 terminal easily enough via this tutorial , but have no way of interacting with it via Python.

    推荐答案

    如何将新文件推送到GitHub?

    一个非常相似的问题,我能够修改谁的代码以通过python将多个文件推送到github:

    A very similar question who's code I was able to modify to make multiple file pushes to github via python:

    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)
    

    这篇关于如何使用python将本地文件推送到github? (或通过Python发布提交)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆