簡體   English   中英

我如何從python shell提交並推送到github?

[英]how do I commit and push to github from python shell?

我在python shell / IDLE中保存了三個.py文件。 我想承諾並將它們推送到我的GitHub帳戶/回購。 這可能嗎? 如果是這樣怎么樣?

要使用python中的shell從macOS(Mac終端)執行此操作,您可以執行以下操作:

#Import dependencies
from subprocess import call

#Commit Message
commit_message = "Adding sample files"

#Stage the file 
call('git add .', shell = True)

# Add your commit
call('git commit -m "'+ commit_message +'"', shell = True)

#Push the new or update files
call('git push origin master', shell = True)

在python中有一個名為GitPython的 git python庫。 另一種方法是從shell(如果你使用的是linux)這樣做。 例如:

from subprocess import call
call('git add .', shell = True)
call('git commit -a "commiting..."', shell = True)
call('git push origin master', shell = True)

您可以使用subprocess模塊執行任意shell命令。

設置要使用的測試文件夾:

$ cd ~/test
$ touch README.md
$ ipython

使用來自IPython的git:

In [1]: import subprocess

In [2]: print subprocess.check_output('git init', shell=True)

Initialized empty Git repository in /home/parelio/test/.git/

In [3]: print subprocess.check_output('git add .', shell=True)

In [4]: print subprocess.check_output('git commit -m "a commit"', shell=True)

[master (root-commit) 16b6499] Initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md

關於shell=True的注釋shell=True

使用不受信任的用戶輸入時,請勿使用shell=True 請參閱Python文檔中警告

還有一點說明:

與編程中的許多事情一樣 - 僅僅因為你並不意味着你應該這樣做。 在這種情況下,我個人的偏好是使用現有的庫而不是自己的庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM