将本地分支推送到远程分支 - gitpython

问题描述 投票:4回答:1

我在我的Github中创建了新的存储库。使用gitpython库我可以获得这个存储库。然后我创建新分支,添加新文件,提交并尝试推送到新分支。

请检查以下代码:

import git
import random
import os

repo_name = 'test'
branch_name = 'feature4'

remote_repo_addr_git = 'git@repo:DevOps/z_sandbox1.git'

no = random.randint(0,1000)
repo = git.Repo.clone_from(remote_repo_addr_git, repo_name)
new_branch = repo.create_head(branch_name)
repo.head.set_reference(new_branch)
os.chdir(repo_name)
open("parasol" + str(no), "w+").write(str(no)) # this is added
print repo.active_branch
repo.git.add(A=True)
repo.git.commit(m='okej')
repo.git.push(u='origin feature4')

一切正常,直到最后推动方法。我收到了这个错误:

stderr:'fatal:'origin feature4'似乎不是一个致命的git存储库:无法从远程存储库中读取。

请确保您拥有正确的访问权限并且存储库已存在。

我可以从命令行运行此方法,它工作正常:

git puth -u origin feature4

但它在python中不起作用。你能告诉我我该怎么办?

python git push gitpython
1个回答
3
投票

这对我有用:

repo.git.push("origin", "feature4")
© www.soinside.com 2019 - 2024. All rights reserved.