我有一个本地的git仓库。我正在使用gitpython库用python提交本地仓库。我想将提交推送到github。如何使用gitpython或任何其他库来完成这个任务?
我在网上查找了解决方法,但没有找到可行的方案。请问有谁可以帮助我吗?
提前致谢。
from git import Repo,remote
rw_dir = 'path/to/your/local/repo'
repo = Repo(rw_dir)
'''Enter code to commit the repository here.
After commit run the following code to push the commit to remote repo.
I am pushing to master branch here'''
origin = repo.remote(name='origin')
origin.push()
以下是使用Python进行GitHub提交和推送的代码:
import subprocess as cmd
def git_push_automation():
try:
cp = cmd.run("file path", check=True, shell=True)
print("cp", cp)
cmd.run('git commit -m "message"', check=True, shell=True)
cmd.run("git push -u origin master -f", check=True, shell=True)
print("Success")
return True
except:
print("Error git automation")
return False
如果您正在使用基于密码的身份验证
import subprocess,os,commands
git_push = " cd /to/the/repo/directory/ ; git add -A ; git commit -m 'my message' ; git push --repo https://<username_here>:<password_here>@bitbucket.org/fullpath/to/your_repo.git --all "
git_push_status = commands.getstatusoutput(git_push)
print(git_push_status[1])
主要是正确地替换存储库URL,例如如果您的存储库和凭据为
username_here
:manjunath
password_here
:password@123
那么git push URL应该像这样,注意密码中存在的特殊字符需要替换(我已经在URL中将密码中的@
替换为%40
)
https://manjunath:password%40123@bitbucket.org/fullpath/to/your_repo.git