2

I have two repositories on github, using gitpython I'm trying to push a file from one repository to another remote repository. I've managed to do it using git but struggling with the gitpython code.

git remote add remote_to_push git@bitbucket...
git fetch remote_to_push
git checkout remote_to_push/master
git add file_to_push
git commit -m "pushing file"
git push remote_to_push HEAD:master

I've managed to create a repo object of the remote I think with the following

from git import Repo
repo = Repo('path/to/other/git/repo')
remote = repo.remotes.origin

I can't figure out how to add something to then push, if i call

remote.add("file_to_push")

Then I get errors about the create() function

TypeError: create() takes exactly 4 arguments (2 given)

Trying to follow what they have done in How to push to remote repo with GitPython with

remote.push(refspec='{}:{}'.format(local_branch, remote_branch))

I assume it should work with using master and master as the remote branches as they both must exist but it's giving me the error

stderr: 'error: src refspec master does not match any.'

Thanks

f23aaz
  • 339
  • 2
  • 13

1 Answers1

1

Solved it. First created a remote of the other repo

git remote add remote_to_push git@bitbucket...

Then the gitpython code

from git import Repo

repo = Repo('path/to/other/git/repo') #create repo object of the other repository
repo.git.checkout('remote_to_push/master') #checkout to a branch linked to the other repo
file = 'path/to/file' #path to file to push
repo.git.add(file) # same as git add file
repo.git.commit(m = "commit message") # same as git commit -m "commit message"
repo.git.push('remote_to_push', 'HEAD:master') # git push remote_to_push HEAD:master

Aside from the docs, I found the following to be quite helpful if anyone's struggling with gitpython as I found it quite a pain

Python Git Module experiences?

http://sandlininc.com/?p=801

Git push via GitPython

How to push to remote repo with GitPython

f23aaz
  • 339
  • 2
  • 13