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")
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
Following is the code to git add
, git commit
and then git push
using GitPython.
Install GitPython using pip install 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()