gpt4 book ai didi

python - 推送到远程仓库

转载 作者:行者123 更新时间:2023-12-01 09:20:08 25 4
gpt4 key购买 nike

我在 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

我已经成功创建了一个远程仓库对象,我认为如下

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)

尝试遵循他们在 How to push to remote repo with GitPython 中所做的事情与

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

How to push to remote repo with GitPython

关于python - 推送到远程仓库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50854924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com