如何通过 ssh 从 Windows 机器进行 git clone?

unh*_*mer 6 windows git ssh

我可以ssh windowsmachine从 Linux 访问 Windows 机器,从那里我可以git init --bare foo.git,告诉我Initialized empty Git repository in C:/Users/unhammer/foo.git/

但是我如何从 unix 端克隆它呢?

$ git clone ssh://windowsmachine:foo.git
Cloning into 'foo'...
fatal: No path specified. See 'man git-pull' for valid url syntax

$ git clone ssh://windowsmachine:C:\\Users\\unhammer\\foo.git
Cloning into '\Users\unhammer\foo'...
fatal: No path specified. See 'man git-pull' for valid url syntax

$ git clone ssh://windowsmachine:/foo.git
Cloning into 'foo'...
fatal: ''/foo.git'' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Run Code Online (Sandbox Code Playgroud)

并类似的消息/C:/Users/unhammer/foo.git/C/Users/unhammer/foo.git/Users/unhammer/foo.git

注意双单引号:

fatal: ''/Users/unhammer/foo.git'' does not appear to be a git repository
Run Code Online (Sandbox Code Playgroud)

当我尝试时不会发生这种情况git clone linuxmachine:/some/path/that/does/not/exist.git,然后 git 使用单引号。(也许这就是问题所在,git-for-windows 或应用额外引号的东西?)

unh*_*mer 9

最简单的解决方案是将默认的 Windows OpenSSH shell 更改为 bash。您可以从 Windows 机器上的 powershell 轻松完成此操作:

powershell
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Program Files\Git\bin\bash.exe" -PropertyType String -Force
Run Code Online (Sandbox Code Playgroud)

您可能必须在 Windows 机器上重新启动 OpenSSH 和/或在它生效之前终止来自客户端的现有 ssh 连接。现在您可以在没有任何额外 -u 选项的情况下进行克隆:

git clone ssh://windowsmachine/c/Users/unhammer/foo.git
Run Code Online (Sandbox Code Playgroud)

(如果您之前指定了 uploadpack/receivepack,请将它们从 .git/config 中删除)