Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git push via GitPython

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
like image 250
amigo Avatar asked Jan 24 '17 19:01

amigo


People also ask

Can I use git with Python?

GitPython is a python library used to interact with git repositories. It is a module in python used to access our git repositories. It provides abstractions of git objects for easy access of repository data, and additionally allows you to access the git repository more directly using pure python implementation.

How do I remote a git repository?

To add a new remote, use the git remote add command on the terminal, in the directory your repository is stored at. The git remote add command takes two arguments: A unique remote name, for example, “my_awesome_new_remote_repo” A remote URL, which you can find on the Source sub-tab of your Git repo.


4 Answers

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()
like image 164
BlackBeard Avatar answered Oct 16 '22 10:10

BlackBeard


You can try the following. It may have your problem solved...

repo.git.pull('origin', new_branch)
repo.git.push('origin', new_branch)
like image 44
Shahaji Avatar answered Oct 16 '22 11:10

Shahaji


This can be achieved by using Index (documented a little bit here) like so:


from git import Repo
repo = Repo('path/to/git/repo')  # if repo is CWD just do '.'

repo.index.add(['bla.txt'])
repo.index.commit('my commit description')
origin = repo.remote('origin')
origin.push()
like image 3
Marc Avatar answered Oct 16 '22 10:10

Marc


Looking at the documentation page of gitpython http://gitpython.readthedocs.io/en/stable/tutorial.html. You have to define a remote repo with something like origin = repo.create_remote('origin', repo.remotes.origin.url)

then origin.pull()

I would look at the whole example in the documentation in the section "Handling Remotes"

Here is the full example from the documentation

empty_repo = git.Repo.init(osp.join(rw_dir, 'empty'))
origin = empty_repo.create_remote('origin', repo.remotes.origin.url)
assert origin.exists()
assert origin == empty_repo.remotes.origin == empty_repo.remotes['origin']
origin.fetch()                  # assure we actually have data. fetch() returns useful information
# Setup a local tracking branch of a remote branch
empty_repo.create_head('master', origin.refs.master)  # create local branch "master" from remote "master"
empty_repo.heads.master.set_tracking_branch(origin.refs.master)  # set local "master" to track remote "master
empty_repo.heads.master.checkout()  # checkout local "master" to working tree
# Three above commands in one:
empty_repo.create_head('master', origin.refs.master).set_tracking_branch(origin.refs.master).checkout()
# rename remotes
origin.rename('new_origin')
# push and pull behaves similarly to `git push|pull`
origin.pull()
origin.push()
# assert not empty_repo.delete_remote(origin).exists()     # create and delete remotes
like image 1
Kabard Avatar answered Oct 16 '22 10:10

Kabard