How to push to remote repo with GitPython
Asked Answered
E

2

7

I have to clone a set of projects from one repository and push it then to a remote repository automatically. Therefore i'm using python and the specific module GitPython. Until now i can clone the project with gitpython like this:

def main():
  Repo.clone_from(cloneUrl, localRepoPath)
  # Missing: Push the cloned repo to a remote repo.

How can i use GitPython to push the cloned repo to a remote repo?

Emelinaemeline answered 2/1, 2017 at 15:35 Comment(0)
M
9

it's all in the documentation:

repo = Repo.clone_from(cloneUrl, localRepopath)
remote = repo.create_remote(remote_name, url=another_url)
remote.push(refspec='{}:{}'.format(local_branch, remote_branch))

see also the push reference API. You can avoid the refspec setting if you set a tracking branch for the remote you want to push to.

Melva answered 2/1, 2017 at 16:16 Comment(2)
This is similar to subprocess.check_call(['git', 'remote', 'set-url', 'remote', url]. You can leave out the url=, no kw arg needed.Vaulting
This is also similar to opening and writing the .git/config file and write the stuff within it. Here the OP asks specifically about GitPython ☺. Though, good point for the url=, even though the documentation itself uses the keyword in several places.Melva
M
5

It should work like this

r = Repo.clone_from(cloneUrl, localRepoPath)
r.remotes.origin.push()

provided that a tracking branch was setup already.

Otherwise you would set a refspec:

r.remotes.origin.push(refspec='master:master')
Mediocre answered 2/1, 2017 at 17:43 Comment(2)
This solution pushes it to origin, but i wanted to push it to a new remote.Emelinaemeline
Does not help because the remote is the same.Vaulting

© 2022 - 2024 — McMap. All rights reserved.