Git push via GitPython
Following is the code to git add
, git commit
and then git push
using GitPython.
Install GitPython using pip install gitpython
.
from git import RepoPATH_OF_GIT_REPO = r'path\to\your\project\folder\.git' # make sure .git folder is properly configuredCOMMIT_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()
This can be achieved by using Index (documented a little bit here) like so:
from git import Reporepo = Repo('path/to/git/repo') # if repo is CWD just do '.'repo.index.add(['bla.txt'])repo.index.commit('my commit description')origin = repo.remote('origin')origin.push()