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()
Answer from Niladri Basu on Stack OverflowFollowing 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()
You can try the following. It may have your problem solved...
repo.git.pull('origin', new_branch)
repo.git.push('origin', new_branch)
git push using python - Stack Overflow
I made a Python script that checks the commit and push status of my important git repositories.
Is there a way to force a git push?
how do I commit and push to github from python shell? - Stack Overflow
Videos
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()
The code which is used to commit and push to the GitHub using python as follows,
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
This script isn't perfect or amazing. It parses git's output for hardcoded english strings instead of using a library, and the directories are hardcoded instead of in a config file. But maybe you will like this idea and expand on it in your own way. Figured I may as well share it rather than not.
https://github.com/voussoir/cmd/blob/master/gitcheckup.py
The output looks like this:
[ ][P] D:\Git\cmd (+0, -0, ~1) [ ][ ] D:\Git\epubfile (+0, -0, ~1) [ ][ ] D:\Git\Etiquette (+0, -0, ~4) [ ][P] D:\Git\reddit (+3, -0, ~45) [ ][P] D:\Git\reddit\Timesearch (+0, -0, ~1) [C][P] D:\Git\sigilplugins [ ][P] D:\Git\voussoirkit (+1, -0, ~0) [C][P] D:\Git\YCDL
[C] means everything has been committed, [P] means the latest commit has been pushed, even if there are yet more uncommitted changes.
It uses my winwhich module (pip install voussoirkit) because my PATH contains a .lnk to git instead of the actual exe. If you don't want that, you can swap it out for shutil.which or hardcode your git executable or whatever.
Have fun!
To do it from macOS (Mac terminal) using the shell from python, you can do this:
#Import dependencies
from subprocess import call
#Commit Message
commit_message = "Adding sample files"
#Stage the file
call('git add .', shell = True)
# Add your commit
call('git commit -m "'+ commit_message +'"', shell = True)
#Push the new or update files
call('git push origin master', shell = True)
There's a python library for git in python called GitPython. Another way to this is doing it from shell(if you're using linux). For Example:
from subprocess import call
call('git add .', shell = True)
call('git commit -a "commiting..."', shell = True)
call('git push origin master', shell = True)
How do I push new files to GitHub?
A very similar question who's code I was able to modify to make multiple file pushes to github via python:
import base64
from github import Github
from github import InputGitTreeElement
user = "GithubUsername"
password = "*********"
g = Github(user,password)
repo = g.get_user().get_repo('git-test') # repo name
file_list = [
'C:\\Users\jesse\Dropbox\Swell-Forecast\git-test\index.html',
'C:\\Users\jesse\Dropbox\Swell-Forecast\git-test\margin_table.html'
]
file_names = [
'index.html',
'margin_table.html'
]
commit_message = 'python commit'
master_ref = repo.get_git_ref('heads/master')
master_sha = master_ref.object.sha
base_tree = repo.get_git_tree(master_sha)
element_list = list()
for i, entry in enumerate(file_list):
with open(entry) as input_file:
data = input_file.read()
if entry.endswith('.png'): # images must be encoded
data = base64.b64encode(data)
element = InputGitTreeElement(file_names[i], '100644', 'blob', data)
element_list.append(element)
tree = repo.create_git_tree(element_list, base_tree)
parent = repo.get_git_commit(master_sha)
commit = repo.create_git_commit(commit_message, tree, [parent])
master_ref.edit(commit.sha)
you can also use repository contents API to push to github.
original answer coming from similar thread.