6

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

1

3 Answers 3

8
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()
Sign up to request clarification or add additional context in comments.

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. From review
3

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

1 Comment

I cant Push using this code. the "file path" is my local file path?
0

If you are using Password-based authentication

import subprocess,os,commands

git_push = " cd /to/the/repo/directory/ ;  git add -A ; git commit -m 'my message' ; git push --repo  https://<username_here>:<password_here>@bitbucket.org/fullpath/to/your_repo.git --all "
  
git_push_status = commands.getstatusoutput(git_push)

print(git_push_status[1])

The main thing is to replace the repo URL properly,for example if your repo and credentials are

username_here : manjunath

password_here : password@123

Then git push URL should look like this, Note that special characters present in the password need to be replaced(I have replaced @ present in password to %40 in the URL)

https://manjunath:password%[email protected]/fullpath/to/your_repo.git

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.