如何将新文件推送到 GitHub?

新手上路,请多包涵

我在 github.com 上创建了一个新的存储库,然后将它克隆到我的本地机器上

git clone https://github.com/usrname/mathematics.git

我在文件夹下添加了3个新文件 mathematics

 $ tree
.
├── LICENSE
├── numerical_analysis
│   └── regression_analysis
│       ├── simple_regression_analysis.md
│       ├── simple_regression_analysis.png
│       └── simple_regression_analysis.py

现在,我想使用 Python 将 3 个新文件上传到我的 GitHub,更具体地说, 是 PyGithub 。这是我尝试过的:

 #!/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 simple regression analysis'

    tree = repo.get_git_tree(sha)
    repo.create_git_commit(commit_message, tree, [])
    repo.push()

if __name__ == '__main__':
    main()

我不知道

  • 如何获取字符串 sha repo.get_git_tree
  • 如何在第 2 步和第 3 步之间建立连接,即推送特定文件

就个人而言, PyGithub 文档 不可读。经过长时间的搜索,我无法找到合适的 api。

原文由 SparkAndShine 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 867
2 个回答

我尝试使用 GitHub API 提交多个文件。 Git 数据 API 的这个页面说它应该“非常简单”。有关该调查的结果,请参阅 此答案

我建议使用类似 GitPython 的东西:

 from git import Repo

repo_dir = 'mathematics'
repo = Repo(repo_dir)
file_list = [
    'numerical_analysis/regression_analysis/simple_regression_analysis.py',
    'numerical_analysis/regression_analysis/simple_regression_analysis.png'
]
commit_message = 'Add simple regression analysis'
repo.index.add(file_list)
repo.index.commit(commit_message)
origin = repo.remote('origin')
origin.push()

注意: 此版本的脚本在存储库的父目录中运行。

原文由 user3657941 发布,翻译遵循 CC BY-SA 3.0 许可协议

注意: 此版本的脚本是从 GIT 存储库内部调用的,因为我从文件路径中删除了存储库名称。

我终于想通了如何使用 PyGithub 提交多个文件:

 import base64
from github import Github
from github import InputGitTreeElement

token = '5bf1fd927dfb8679496a2e6cf00cbe50c1c87145'
g = Github(token)
repo = g.get_user().get_repo('mathematics')
file_list = [
    'numerical_analysis/regression_analysis/simple_regression_analysis.png',
    'numerical_analysis/regression_analysis/simple_regression_analysis.py'
]
commit_message = 'Add simple regression analysis'
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 entry in file_list:
    with open(entry, 'rb') as input_file:
        data = input_file.read()
    if entry.endswith('.png'):
        data = base64.b64encode(data)
    element = InputGitTreeElement(entry, '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)
""" An egregious hack to change the PNG contents after the commit """
for entry in file_list:
    with open(entry, 'rb') as input_file:
        data = input_file.read()
    if entry.endswith('.png'):
        old_file = repo.get_contents(entry)
        commit = repo.update_file('/' + entry, 'Update PNG content', data, old_file.sha)

如果我尝试从 PNG 文件添加原始数据,对 create_git_tree 的调用最终会调用 json.dumpsRequester.py --- 中调用 --- ,这会引发以下异常:

UnicodeDecodeError: 'utf8' codec can't decode byte 0x89 in position 0: invalid start byte

我通过 base64 编码 PNG 数据并提交来解决这个问题。后来,我使用 update_file 方法更改PNG数据。这会导致两次单独提交到存储库,这可能不是您想要的。

原文由 user3657941 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏