[英]how to push from Pycharm to Github files that I copy/paste on the project?
[英]How do I push new files to GitHub?
我在 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()
我不知道
repo.get_git_tree
的字符串sha
就個人而言, PyGithub 文檔不可讀。 找了好久都沒找到合適的api。
我嘗試使用GitHub API提交多個文件。 Git Data 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()
注意:此版本的腳本在存儲庫的父目錄中運行。
注意:此版本的腳本是從 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.dumps
在Requester.py
,這將導致以下異常升高:
UnicodeDecodeError: 'utf8' codec can't decode byte 0x89 in position 0: invalid start byte
我通過對 PNG 數據進行base64
編碼並提交來解決這個問題。 后來,我使用update_file
方法來更改 PNG 數據。 這會導致對存儲庫的兩次單獨提交,這可能不是您想要的。
我可以給你一些信息支持,但也有一個具體的解決方案。
在這里您可以找到將新文件添加到存儲庫的示例, 這里有一個視頻教程。
您可以在下面看到在 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)
使用子流程,這將完成相同的工作-
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 內的文件。
import subprocess
p = subprocess.Popen("git rev-parse HEAD".split(), stdout=subprocess.PIPE)
out, err = p.communicate()
sha = out.strip()
使用 PyGithub 可能有一種方法可以做到這一點,但這應該適用於快速破解。
如果您不需要 pygithub,dulwich git-library 提供了高級 git 命令。 有關命令,請查看https://www.dulwich.io/apidocs/dulwich.porcelain.html
如果 PyGithub 的文檔不可用(而且看起來不像),而您只想推送提交(不對問題、存儲庫配置等做任何花哨的事情),您可能最好直接與 git 交互,要么調用git
可執行文件,要么使用包裝庫,例如GitPython 。
直接將git
與諸如subprocess.Popen
東西一起使用在傾斜曲線上可能會更容易,但從長遠來看,錯誤處理等也會更困難,因為您實際上沒有很好的抽象可以傳遞,並且將不得不自己進行解析。
擺脫 PyGithub 還可以讓您擺脫與 GitHub 及其 API 的綁定,允許您推送到任何存儲庫,甚至是計算機上的另一個文件夾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.