Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git push using python

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

like image 501
Sam Avatar asked Jan 30 '17 23:01

Sam


People also ask

How do I push PY files to GitHub?

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.

How do I git with Python?

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.


2 Answers

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()
like image 183
Sam Avatar answered Sep 21 '22 10:09

Sam


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
like image 43
Sashini Hettiarachchi Avatar answered Sep 21 '22 10:09

Sashini Hettiarachchi