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

杰西·雷扎·霍拉萨尼(Jesse Reza Khorasanee)

有哪些选项可用于将文件从python提交和推送到github?

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

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

  2. 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 "
    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')`
    
  3. git push从python子进程(SSH)在命令行中使用我在这里遇到的问题是,我找不到在Windows命令提示符下创建ssh代理的方法。通过本教程我已经能够在MINGW64终端中轻松地创建一个,但是无法通过Python与之交互。

杰西·雷扎·霍拉萨尼(Jesse Reza Khorasanee)

如何将新文件推送到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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用 maven 发布插件将发布推送到 github?

如何使git工作通过tor将提交推送到GitHub?

无法将提交从本地分支推送到github存储库

如何将代码从本地 git 推送到 github,不包括媒体文件夹?

如何将 Eclipse 项目提交并推送到 GitHub?

修改提交后如何将分支推送到github?

我如何将提交推送到已经从本地存储库推送到 git 的 heroku?

如何将本地 git reset 推送到 github?

如何使用.git提交子文件夹并将其推送到github?

无法通过PhpStorm [MacOs]将文件推送到GitHub

如何使用python将现有文件推送到gitlab存储库

更改推送到 GitHub 的提交...删除文件

无法将提交推送到GitHub

无法将提交推送到远程GitHub

如何使用python将CSV数据推送到mongodb

如何使用Python / pyodbc将数据推送到Google表格?

如何使用angular js将所有值从txt文件推送到本地存储

在本地将GitHub Repository推送到master分支:不推送文件夹

将所有文件推送到我的 github 后如何修复 git 提交的身份不明的用户

如何使用 python bot 将本地 html 文件发送到 Telegram

如何将新文件推送到GitHub?

使用 curl 将文件推送到 GitHub 存储库

使用PyGithub和Python将本地git存储库推送到用户的远程git存储库

将本地 repo 从 master 更改为 main 以将文件推送到 github 上的 master repo

如何使用Android Studio将子模块推送到github?

如何通过Webhooks从Gitlab推送到Github

当我将项目推送到Github时,我拥有最早提交的文件,为什么?

将文件更新推送到 GitHub

如何将本地分支 A 中的多个本地提交推送到远程分支 B 并从本地分支 A 中清除它们?