我使用GitPython初始化新的本地存储库,创建初始提交并推送到规范存储库。不幸的是,最后一步失败了,我在理解原因方面遇到了很多麻烦。我确定我只是错误地使用GIT_SSH_COMMAND
变量,但我不确定如何。目前还没有很多例子可以继续。
我已阅读this SO question并挖掘了相关的issue和commit,但我显然无法正确管理它们。
帮助?
"证明" Git v2.3 +
$ git --version
git version 2.3.1
脚本代码段
# Please take my word that I've init'd the repo and that any variables
# have been defined and initialized.
git_ssh_identity_file = os.path.expanduser('~/.ssh/id_rsa')
git_ssh_cmd = 'ssh -i %s' % git_ssh_identity_file
with git_project.git.custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
git_project.remotes.origin.push(git_project.heads.master)
产生的错误
Traceback (most recent call last):
File "./ct-new-project.py", line 204, in <module>
git_project.remotes.origin.push(git_project.heads.master)
File "/Library/Python/2.7/site-packages/git/remote.py", line 667, in push
return self._get_push_info(proc, progress or RemoteProgress())
File "/Library/Python/2.7/site-packages/git/remote.py", line 588, in _get_push_info
handle_process_output(proc, stdout_handler, progress_handler, finalize_process)
File "/Library/Python/2.7/site-packages/git/cmd.py", line 202, in handle_process_output
return finalizer(process)
File "/Library/Python/2.7/site-packages/git/util.py", line 158, in finalize_process
proc.wait()
File "/Library/Python/2.7/site-packages/git/cmd.py", line 300, in wait
raise GitCommandError(self.args, status, self.proc.stderr.read())
git.exc.GitCommandError: 'git push --porcelain origin master' returned with exit code 128
答案 0 :(得分:1)
当GitPython抛出这种错误时,总是值得检查它尝试执行的实际命令是否可以从命令行运行。您的本地克隆中的某些内容可能已更改,导致命令无法成功完成。
使用GitPython可以实现的功能相当于以下内容:
$ GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa' git push --porcelain origin master
至少在LINUX上。在Windows上,您可能需要直接在单独的命令中设置环境变量。
在进行这样的实验时,我觉得有一个“本地”上游我可以推到我硬盘上的某个位置,以便我可以把它扔掉并重新启动 - 或git push --force
从一秒钟(原始)上游的克隆..因为你只是知道你至少要弄乱它一次。