如何通过gitpython添加/提交和推送存储库?

2024-04-23 10:22:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用git python添加、提交和推送到存储库。根据不完整的文档和示例here,我尝试了以下方法:

myrepo = Repo('Repos/hello-world/.git')
# make changes to README.md
myrepo.index.add('README.md')
myrepo.index.commit("Updating copyright year")
myrepo.git.push("origin", "copyright_updater")   ###

我签出了存储库hello_world,并将其放在一个文件夹Repos下。我确实更改了一个文件README.md。但是有了这个代码,我得到了一个错误

git.exc.GitCommandError: Cmd('git') failed due to: exit code(1)
   cmdline: git push origin copyright_updater
   stderr: 'error: src refspec copyright_updater does not match any.
 error: failed to push some refs to 'git@github.com:alex4200/hello-world.git''

在标记行中

如何修复它,以便将更改推送到新分支并在GitHub上创建拉请求


Tags: togithelloworldindexerrororiginpush
1条回答
网友
1楼 · 发布于 2024-04-23 10:22:34

您需要做的是直接使用git。这在gitpython教程的末尾解释

基本上,当您有一个repo对象时,您可以像

repo.git.function(param1, param2, param3, ...) 

例如,调用git命令

git push  set-upstream origin testbranch

是吗

repo.git.push(" set-upstream", "origin", "testbranch")

适用有关“-”的特殊规则

因此,为了创建一个新分支并将其推送到github,完整的序列变得

repo = Repo('Repos/hello-world/.git')
# make changes to README.md
repo.index.add('README.md')
repo.index.commit("My commit message")
repo.git.checkout("-b", "new_branch")
repo.git.push(" set-upstream","origin","new_branch")

如何在github上为新分支创建拉取请求,是我还没有掌握的一些不同的魔法

相关问题 更多 >