There's a better way to achieve your goal. While the question and various answers are interesting and instructive, the best way to achieve the goal does not involve the GIT_SSH_COMMAND variable.
Instead of putting the information in "git", put the information into "ssh".
The "ssh" command has always had a "config" file in Unix/Linux. That capability is available in Windows, also, at least in recent years. Configure "ssh" to know about your identity file, and then when you go to perform the "git clone" command...
git clone <repository>
...specify the <repository>
in the style of ssh protocol. Here is the ssh protocol style, as described on the "git" reference page for "git clone", at the sub-header "GIT URLs":
[user@]host.xz:path/to/repo.git/
Note that there is no scheme specifier; which is to say there is no indication of ssh:
nor git:
nor https:
nor ftps:
. Instead, the colon :
is separating "host" from "path". This use of the colon :
does not conform to URL format, and the non-conformance distinguishes this protocol style from the others.
A potentially important benefit of doing the configuration within "ssh" is that "ssh" allows alias names. Suppose, for "ssh" sessions to a particular machine, I normally want to login as myself, but sometimes I want to login as somebody else, named "user_for_git". Further suppose that the identity files for the users are different. The following "config" for "ssh" has entries that are named by the real domain for myself, but with an alias for the "user_for_git". Note that the keyword "Host" introduces a section, while the variable "HostName" is one of several parameters within the section.
Host go_git.example.com
# Specify the real host name
HostName = example.com
User = user_for_git
IdentityFile = ~/.ssh/id_rsa_for_git_at_example_com
Host example.com
# Specify the real host name
HostName = example.com
User = user_me
IdentityFile = ~/.ssh/id_rsa_for_normal
With that in place, the "git clone" command can be as follows:
git clone go_git.example.host:/path/to/repo.git
Note that in this case the optional user@
portion of the syntax is not used. The remote repository is specified by just host : path
, but in this case the host
is an alias defined in the "ssh" config file.
In Linux/Unix, the "ssh" config file is named config
and is in the hidden directory .ssh
, which is located under the home directory $HOME
; this is expressed as ~/.ssh/config
. For the equivalent location(s) within Windows, check the answers here: https://stackoverflow.com/a/62842368/3552393