通过GitPython进行Git推送 [英] Git push via GitPython

查看:98
本文介绍了通过GitPython进行Git推送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中有以下代码(使用"import git"):

I have this code in Python (using "import git"):

repo = git.Repo("my_repository")
repo.git.add("bla.txt")
repo.git.commit("my commit description")

现在我要推送此提交.我已经尝试了很多,但都没有成功. Python命令应类似于此Bash命令:

Now I want to push this commit. I've tried a lot with no success. The Python command should be similar to this Bash command:

git push origin HEAD:refs/for/master

推荐答案

以下是使用使用pip install gitpython安装 GitPython .

from git import Repo

PATH_OF_GIT_REPO = r'path\to\your\project\folder\.git'  # make sure .git folder is properly configured
COMMIT_MESSAGE = 'comment from python script'

def git_push():
    try:
        repo = Repo(PATH_OF_GIT_REPO)
        repo.git.add(update=True)
        repo.index.commit(COMMIT_MESSAGE)
        origin = repo.remote(name='origin')
        origin.push()
    except:
        print('Some error occured while pushing the code')    

git_push()

这篇关于通过GitPython进行Git推送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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