推送到远程仓库

Push to remote repository

我在 github 上有两个存储库,使用 gitpython 我正在尝试将一个文件从一个存储库推送到另一个远程存储库。我已经设法使用 git 做到了,但在使用 gitpython 代码时遇到了困难。

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

我已经成功地创建了一个遥控器的 repo 对象,我想是这样

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

如果我调用

,我不知道如何添加内容然后推送
remote.add("file_to_push")

然后我得到关于 create() 函数的错误

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

试图用

跟进他们在 中所做的事情
remote.push(refspec='{}:{}'.format(local_branch, remote_branch))

我认为它应该使用 master 和 master 作为远程分支,因为它们都必须存在,但它给了我错误

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

谢谢

解决了。 首先创建了另一个仓库的远程

git remote add remote_to_push git@bitbucket...

然后是gitpython代码

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

除了文档之外,我发现如果有人在 gitpython 上苦苦挣扎,我发现以下内容非常有用,因为我发现它很痛苦

Python Git Module experiences?

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

Git push via GitPython