I have local git repository. I am using python to commit the local repo using gitpython library. I want to push the commit to github. How can I do this using gitpython or any other library. I looked online but there was no solution available. Can anyone help me with this. Thanks in advance
On GitHub.com, navigate to the main page of the repository. Above the list of files, using the Add file drop-down, click Upload files. Drag and drop the file or folder you'd like to upload to your repository onto the file tree.
We can use git module in python to clone the repository from git. Clone the repository you want to work with in local system. So in clone_from methods pass the two arguments in which first argument is url of your repository and second argument is the location of your directory where you want to cloned the repo.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With