问题
When I clone from GitHub using CMD and PowerShell with ssh-agent on Windows 10 v.1909, the following shows:
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
However, I ran ssh -vT git@github.com
and the key does work
debug1: Offering public key: {My key}
And
Hi {My username}! You've successfully authenticated, but GitHub does not provide shell access.
But with Git bash I can clone just fine.
This also happen with GitLab.
I tried
Host *
ForwardAgent yes
in .ssh/config
and it doesn't work.
I also tried every solution from GitHub Support Page and GitHub Community Forum and still doesn't work.
PS. I prefer Windows cmd.
回答1:
In your Powershell session, try:
$env:GIT_SSH_COMMAND='ssh -Tv' git clone git@gitlab.com:myuser/myrepo.git
And see where SSH is looking for your default id_rsa/id_rsa.pub key pair.
Make sure, if the private key is passphrase-protected, to launch ssh-agent first.
The OP mentions:
Apparently, Git doesn't use native OpenSSH.
That is false. Maybe GitHub Desktop does not use OpenSSH, as seen in desktop/desktop issue 5641: "Desktop does not use OpenSSH on Windows if running, favours embedded SSH"
Hence the workaround:
git config --global core.sshCommand "'C:\Windows\System32\OpenSSH\ssh.exe'"
But Git itself does:
D:\prgs\gits\current\bin>where ssh
D:\prgs\gits\current\usr\bin\ssh.exe
D:\prgs\gits\current\bin>ssh -V
OpenSSH_8.2p1, OpenSSL 1.1.1f 31 Mar 2020
This is more recent than the Windows one:
C:\WINDOWS\System32\OpenSSH\ssh.exe -V
OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.5
(Winver: 1909, build 18363.836=
That is why I always launch tools with my own PATH
set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\
set GH=C:\path\to\git
set PATH=%GH%\bin;%GH%\usr\bin;%GH%\mingw64\bin;%PATH%
That way, I am sure I will use Git tools first (incuding an OpenSSH one) before anything else.
回答2:
It turns out that git doesn't use native OpenSSH. Here what's I did
git config --global core.sshCommand "'C:\Windows\System32\OpenSSH\ssh.exe'"
Reference here
来源:https://stackoverflow.com/questions/61525234/permission-failure-cloning-in-git-in-windows