Ale*_*lex 5 python git gitpython
我正在尝试使用 git-python 添加、提交并推送到存储库。根据不完整的文档和此处的示例,我尝试了以下方式:
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") ###
Run Code Online (Sandbox Code Playgroud)
我检查了存储库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''
Run Code Online (Sandbox Code Playgroud)
在标记线上。
我该如何修复它,以便将更改推送到新分支并在 GitHub 上创建拉取请求?
您需要做的就是直接使用git。这在gitpython教程的末尾有解释。
基本上,当你有一个repo对象时,你可以调用每个 git 函数,例如
repo.git.function(param1, param2, param3, ...)
Run Code Online (Sandbox Code Playgroud)
例如,调用 git 命令
git push --set-upstream origin testbranch
Run Code Online (Sandbox Code Playgroud)
你做
repo.git.push("--set-upstream", "origin", "testbranch")
Run Code Online (Sandbox Code Playgroud)
适用有关“-”的特殊规则。
因此,为了创建一个新分支并将其推送到 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")
Run Code Online (Sandbox Code Playgroud)
如何在 github 上为新分支创建拉取请求,是我尚未掌握的一些不同的魔法......