Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push to remote repo with GitPython

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?

like image 835
Oni1 Avatar asked Jan 02 '17 15:01

Oni1


People also ask

How do I push code into remote repository?

To push the commit from the local repo to your remote repositories, run git push -u remote-name branch-name where remote-name is the nickname the local repo uses for the remote repositories and branch-name is the name of the branch to push to the repository. You only have to use the -u option the first time you push.

How do I push a git repository to a remote server?

In order to push a Git branch to remote, you need to execute the “git push” command and specify the remote as well as the branch name to be pushed. If you are not already on the branch that you want to push, you can execute the “git checkout” command to switch to your branch.

How do I pull changes from remote repo to local repository?

Fetching changes from a remote repositoryUse git fetch to retrieve new work done by other people. Fetching from a repository grabs all the new remote-tracking branches and tags without merging those changes into your own branches. Otherwise, you can always add a new remote and then fetch.


2 Answers

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.

like image 175
zmo Avatar answered Sep 16 '22 12:09

zmo


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')
like image 31
Byron Avatar answered Sep 18 '22 12:09

Byron