Below is the code for git add, git commitand then git pushusing GitPython .
Install GitPython using pip install gitpython.
from git import Repo
PATH_OF_GIT_REPO = r'path\to\your\project\folder\.git'
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')
finally:
print('Code push from script succeeded')
git_push()
source
share