当使用gitpython时,如何设置git用户名和密码?

6
我计划在我的项目中使用GitPython。当我测试它时,使用以下代码,我收到一个错误。
repo.index.add(['*']) 
repo.index.commit(message="Initial Commit")
repo.remotes.origin.push()

错误为:
Traceback (most recent call last):
  File "test.py", line 24, in <module>
    repo.remotes.origin.push()
  File "C:\Python27\lib\site-packages\git\remote.py", line 627, in push
    return self._get_push_info(proc, progress or RemoteProgress())
  File "C:\Python27\lib\site-packages\git\remote.py", line 564, in _get_push_info
    finalize_process(proc)
  File "C:\Python27\lib\site-packages\git\remote.py", line 64, in finalize_process
    proc.wait()
  File "C:\Python27\lib\site-packages\git\cmd.py", line 100, in wait
    raise GitCommandError(self.args, status, self.proc.stderr.read())
git.exc.GitCommandError: 'git push --porcelain origin' returned exit status 128:

在最后一行之后没有任何消息。但是,如果我从命令行手动运行git push --porcelain origin,我会收到以下错误:

fatal: could not read Username for 'https://github.com': No such file or directory

这是一个公平的错误。这是一个全新的代码库,我还没有完全配置好。不过,我计划在多台机器上部署这个项目(包括推送功能),因此我希望能够通过GitPython自动完成。

我该如何设置用户名和密码(或使用SSH密钥)以便将代码推送到远程代码库?


你最终解决了这个问题吗? - user319862
@user319862 我没有。不好意思。 - NewGuy
没有实际使用的完整URL,很难复现。据我所知,只要URL有效,GitHub身份验证就可以正常工作。 - Byron
1个回答

0

你需要手动至少推送一次(即 git push -u origin <branch_name>),以便在第一次执行此操作后,远程端识别你的本地仓库。之后你就不会再遇到这个问题了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接