How do I git clone from a Windows machine over ssh?

Viewed 6567

I can do ssh windowsmachine from Linux to access a Windows machine, and from there I can git init --bare foo.git, telling me Initialized empty Git repository in C:/Users/unhammer/foo.git/

but how do I clone that from the unix side?

$ 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.

and similar messages for /C:/Users/unhammer/foo.git and /C/Users/unhammer/foo.git and /Users/unhammer/foo.git.

Note the double single-quotes:

fatal: ''/Users/unhammer/foo.git'' does not appear to be a git repository

This doesn't happen when I try to git clone linuxmachine:/some/path/that/does/not/exist.git, then git uses single single-quotes. (Maybe that's the issue, git-for-windows or something applying extra quotes?)

2 Answers

The easiest solution is to change the default Windows OpenSSH shell to bash. You can do this easily from powershell on the Windows machine:

powershell
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Program Files\Git\bin\bash.exe" -PropertyType String -Force

You may have to restart OpenSSH on the Windows machine and/or kill existing ssh connections from the client before it takes effect. Now you can clone without any extra -u option:

git clone ssh://windowsmachine/c/Users/unhammer/foo.git

(and if you previously specified uploadpack/receivepack, remove them from .git/config)

Following @phd's link to https://stackoverflow.com/a/8050564/7976758 I found a reference to https://stackoverflow.com/a/2323826/69663 with a workaround to the quoting issue. Here's what I did:

On Windows, open Git Bash and, in my homedir:

echo 'git-receive-pack "$@"' >grp.sh
echo 'git-upload-pack "$@"' >gup.sh

On Linux, clone specifying upload-pack:

git clone -u '"C:/Program Files/Git/bin/bash.exe" gup.sh' ssh://windowsmachine/c/Users/unhammer/foo.git
cd foo
git config remote.origin.uploadpack '"C:\Program Files\Git\bin\bash.exe" gup.sh'
git config remote.origin.receivepack '"C:\Program Files\Git\bin\bash.exe" grp.sh'

So the repo path is the one that Git Bash shows (/c/Users/$username/$repo), no colon anywhere.


https://github.com/PowerShell/Win32-OpenSSH/issues/1082 seems related.

Related