如何将新文件推送到GitHub? [英] How do I push new files to GitHub?

查看:119
本文介绍了如何将新文件推送到GitHub?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a new repository on github.com and then cloned it to my local machine with

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

我在文件夹mathematics

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

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

Now, I'd like to upload 3 new files to my GitHub using Python, more specifically, PyGithub. Here is what I have tried:

#!/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()

我不知道

  • 如何获取repo.get_git_tree的字符串sha
  • 如何在第2步和第3步之间建立连接,即推送特定文件
  • how to get the string sha for repo.get_git_tree
  • how do I make a connection between step 2 and 3, i.e. pushing specific files

就个人而言, PyGithub文档不可读.长时间搜索后,我找不到正确的api.

Personally, PyGithub documentation is not readable. I am unable to find the right api after searching for long time.

推荐答案

我尝试使用 GitHub API 提交多个文件. Git Data API 的页面说,它应该非常简单".有关该调查的结果,请参见此答案.

I tried to use the GitHub API to commit multiple files. This page for the Git Data API says that it should be "pretty simple". For the results of that investigation, see 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()

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

Note: This version of the script was run in the parent directory of the repository.

这篇关于如何将新文件推送到GitHub?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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