如何使用python将本地文件推送到github?(或通过 Python 发布提交)

How to push local files to github using python? (or post a commit via Python)(如何使用python将本地文件推送到github?(或通过 Python 发布提交))
本文介绍了如何使用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:\UsersDropboxgit-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: 我遇到的问题是我找不到创建 ssh 的方法Windows 命令提示符中的代理.我已经能够通过 本教程 ,但无法通过 Python 与之交互.

  • 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') # repo name
    file_list = [
        'C:\UsersjesseDropboxSwell-Forecastgit-testindex.html',
        'C:\UsersjesseDropboxSwell-Forecastgit-testmargin_table.html'
    ]
    file_names = [
        'index.html',
        'margin_table.html'
    ]
    commit_message = 'python commit'
    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'): # images must be encoded
            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 发布提交)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

  • 本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

    相关文档推荐

    Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
    How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
    subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
    I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
    Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
    How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)