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

New*_*Guy 6 python git gitpython

我计划在我的项目中使用 GitPython。当我测试它时,使用此代码我收到一个错误。

repo.index.add(['*']) 
repo.index.commit(message="Initial Commit")
repo.remotes.origin.push()
Run Code Online (Sandbox Code Playgroud)

错误是:

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:
Run Code Online (Sandbox Code Playgroud)

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

fatal: could not read Username for 'https://github.com': No such file or directory
Run Code Online (Sandbox Code Playgroud)

这是一个公平的错误。这是一个全新的存储库,我还没有完全配置。但是,我计划在多台机器上部署这个项目(以及推送的能力),所以我更希望能够通过 GitPython 自动执行。

如何设置用户名和密码(或使用 SSH 密钥)以推送到远程存储库?

小智 0

您需要手动推送至少一次(即 git push -u origin <branch_name>),以便您的本地存储库在第一次执行此操作后在远程端被识别,您不会再次看到此问题