How to push to remote repo with GitPython

孤人 提交于 2019-12-14 03:46:58

问题


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?


回答1:


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.




回答2:


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')


来源:https://stackoverflow.com/questions/41429525/how-to-push-to-remote-repo-with-gitpython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!