如何将新文件推送到GitHub?

时间:2016-07-26 15:59:16

标签: python git github pygithub

我在github.com上创建了一个新的存储库,然后使用

将其克隆到我的本地计算机
git clone https://github.com/usrname/mathematics.git

我在文件夹mathematics

下添加了3个新文件
$ 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 documentation不可读。经过长时间的搜索,我无法找到合适的api。

7 个答案:

答案 0 :(得分:13)

我尝试使用GitHub API提交多个文件。 Git Data API的这个页面说它应该是#34;非常简单"。有关调查结果,请参阅this answer

我建议使用类似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()

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

答案 1 :(得分:4)

注意:此版本的脚本是从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的调用最终会调用Requester.py中的json.dumps,这会导致引发以下异常:

  

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

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

答案 2 :(得分:1)

我可以给你一些信息支持,也是一个具体的解决方案。

Here您可以找到将新文件添加到存储库的示例,here是一个视频教程。

下面你可以看到在GitHub的开发者页面上找到的与GitHub一起使用的python包列表:

但是如果需要,你也可以在IPython中用命令推送文件:

In [1]: import subprocess
In [2]: print subprocess.check_output('git init', shell=True)
Initialized empty Git repository in /home/code/.git/
In [3]: print subprocess.check_output('git add .', shell=True)
In [4]: print subprocess.check_output('git commit -m "a commit"', shell=True)

答案 3 :(得分:1)

如果PyGithub的文档不可用(并且它看起来不是这样),并且你只想推送一个提交(没有对问题,repo配置等做任何想象),你会可能最好直接与git接口,调用B可执行文件或使用包含库(如GitPython)。

git直接用于您提到的git之类的内容可能会更容易在倾斜曲线上进行,但从长远来看也更难以进行错误处理等等,因为您不会这样做。 ;确实有很好的抽象来传递,并且必须自己进行解析。

摆脱PyGithub还可以让你免于被绑定到GitHub及其API,允许你推送到任何仓库,甚至计算机上的另一个文件夹。

答案 4 :(得分:1)

使用子进程,这将做同样的工作-

import subprocess
subprocess.call(['git', 'add', '-A'])
subprocess.call(['git', 'commit', '-m', '{}'.format(commit_message)])
subprocess.call(['git', 'push', 'https://{}@github.com/user-name/repo.git'.format(token)])

确保使用 -A 或 -all 来跟踪项目/甚至父目录中的所有文件。使用 'git add .'将仅跟踪写入此代码的 cwd 内的文件。

答案 5 :(得分:0)

import subprocess
p = subprocess.Popen("git rev-parse HEAD".split(), stdout=subprocess.PIPE)
out, err = p.communicate()
sha = out.strip()

可能有一种方法可以用PyGithub做到这一点,但这应该可以快速入侵。

答案 6 :(得分:0)

如果您不需要专门的pygithub,那么dulwich git-library会提供high level git commands。有关命令,请查看https://www.dulwich.io/apidocs/dulwich.porcelain.html

相关问题