我正在使用gitpython
库从python脚本执行git命令。
当我执行git pull时,它失败并给出以下错误:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic)
。
但是,当我直接从shell运行git pull时,它会成功运行。
除此之外,其他git命令(例如git status
,git log
)也可以正常工作。只有git pull / push会出现上述错误。
这是python脚本:
import os
import git
g = git.cmd.Git(local_repo_path)
os.chdir(local_repo_path)
g.checkout('master') // this works fine
msg = g.pull()
print msg // gives an error mentioned below
输出为:
git.exc.GitCommandError: Cmd('git') failed due to: exit code(1)
cmdline: git pull
stderr: 'Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.'
可能是什么原因?
答案 0 :(得分:0)
这里的问题是用户身份验证。 Shell使用全局git用户名进行身份验证,因此可以正常工作。但是,python脚本git命令使用repo用户名,该用户名与预期的git用户名不同。因此,为python脚本显式设置用户名将起作用。 在本地git存储库中,
运行:
git config -e
在此添加:
[user]
name = yourname
email = youremail
这将确保您的python脚本使用此信息。