使用Python进行git push

6
我有一个本地的git仓库。我正在使用gitpython库用python提交本地仓库。我想将提交推送到github。如何使用gitpython或任何其他库来完成这个任务? 我在网上查找了解决方法,但没有找到可行的方案。请问有谁可以帮助我吗? 提前致谢。

1
https://gitpython.readthedocs.io/en/stable/reference.html#git.remote.PushInfo - hjpotter92
3个回答

8
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()

8
虽然这段代码可能解决了问题,但包括解释真的有助于提高您的帖子质量。请记住,您正在回答面向未来读者的问题,而这些人可能不知道您提供代码建议的原因。 来自审查 - Ferrybig

3

以下是使用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

我无法使用这段代码进行推送。 "文件路径" 是我的本地文件路径吗? - Daniel

0

如果您正在使用基于密码的身份验证

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_heremanjunath

password_herepassword@123

那么git push URL应该像这样,注意密码中存在的特殊字符需要替换(我已经在URL中将密码中的@替换为%40

https://manjunath:password%40123@bitbucket.org/fullpath/to/your_repo.git


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接