It appears you are set up to deploy to GitHub and as such that requires you to have a GitHub user set so it knows where to deploy to. Depending on your environment you need to set that environment variable before running the command:
export GIT_USER=yourusername
Answer from James Gray on Stack OverflowGit clone with custom SSH using GIT_SSH error - Stack Overflow
git - Set GIT_SSH Environment Variable in Gitconfig - Stack Overflow
Newest 'environment-variables' Questions - Page 3 - Stack Overflow
version control - "Cannot spawn ssh" when connecting to Github, but ssh -T git@github.com works? - Stack Overflow
You cannot provide options in the GIT_SSH environment variable; from the git man page:
GIT_SSH
If this environment variable is set then git fetch and git push will use this command instead of ssh when they need to connect
to a remote system. The $GIT_SSH command will be given exactly two arguments: the username@host (or just host) from the URL
and the shell command to execute on that remote system.
To pass options to the program that you want to list in GIT_SSH you will need to wrap the program and options into a shell
script, then set GIT_SSH to refer to the shell script.
One option is to add a stanza to your .ssh/config file with the appropriate configuration:
Host bitbucket.org
StrictHostKeyChecking no
IdentityFile /home/me/my_private_key
Another option is to point GIT_SSH to a shell script that does what you want. E.g., in /home/me/bin/bitbucket_ssh, put:
#!/bin/sh
exec /usr/bin/ssh -o StrictHostKeyChecking=no -i /home/me/my_private_key "$@"
And then point GIT_SSH at /home/me/bin/bitbucket_ssh.
I prefer using .ssh/config when possible, because this avoids the need to create a per-destination script for each remote.
Note that starting with git 2.3+ (Q1 2015), what you initially tried would work, with the new environment variable GIT_SSH_COMMAND.
See commit 3994276 from Thomas Quinot (quinot):
git_connect: set ssh shell command in GIT_SSH_COMMAND
It may be impractical to install a wrapper script for
GIT_SSHwhen additional parameters need to be passed.
Provide an alternative way of specifying a shell command to be run, including command line arguments, by means of theGIT_SSH_COMMANDenvironment variable, which behaves likeGIT_SSHbut is passed to the shell.The special circuitry to modify parameters in the case of using PuTTY's plink/tortoiseplink is activated only when using
GIT_SSH; in the case of usingGIT_SSH_COMMAND, it is deliberately left up to the user to make any required parameters adaptation before calling the underlying ssh implementation.
GIT_SSH_COMMAND:
If either of these environment variables is set then '
git fetch' and 'git push' will use the specified command instead of 'ssh' when they need to connect to a remote system.
The command will be given exactly two or four arguments:
- the '
username@host' (or just 'host') from the URL and the shell command to execute on that remote system, optionally preceded by '-p' (literally) and- the '
port' from the URL when it specifies something other than the defaultSSHport.
$GIT_SSH_COMMANDtakes precedence over$GIT_SSH, and is interpreted by the shell, which allows additional arguments to be included.
$GIT_SSHon the other hand must be just the path to a program (which can be a wrapper shell script, if additional arguments are needed).
In ~/.ssh/config, add:
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa_github
If the config file is new, check access permissions using
stat -c %a ~/.ssh/config
if returns NOT 600, you should do
chmod 600 ~/.ssh/config
Now you can do git clone [email protected]:{ORG_NAME}/{REPO_NAME}.git
- Where
{ORG_NAME}is your GitHub user account (or organization account)'s GitHub URI name.- Note that there is a colon
:aftergithub.cominstead of the slash/- as this is not a URI.
- Note that there is a colon
- And
{REPO_NAME}is your GitHub repo's URI name - For example, for the Linux kernel this would be
git clone [email protected]:torvalds/linux.git).
NOTE: On Linux and macOS, verify that the permissions on your IdentityFile are 400. SSH will reject, in a not clearly explicit manner, SSH keys that are too readable. It will just look like a credential rejection. The solution, in this case, is:
chmod 400 ~/.ssh/id_rsa_github
Environment variable GIT_SSH_COMMAND
From Git version 2.3.0, you can use the environment variable GIT_SSH_COMMAND like this:
GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example" git clone example
Note that -i can sometimes be overridden by your config file, in which case, you should give SSH an empty config file, like this:
GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example -F /dev/null" git clone [email protected]:example/example.git
Configuration core.sshCommand
Since Git version 2.10.0, you can configure this per repo or globally, using the core.sshCommand setting. There's no more need to use the environment variable. Here's how you clone a repo and set this configuration at the same time:
git clone -c "core.sshCommand=ssh -i ~/.ssh/id_rsa_example -F /dev/null" [email protected]:example/example.git
cd example/
git pull
git push
If the repo already exists, run:
git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"
The configuration is saved in .git/config
Since my 2013 answer (Git 1.8.3.4), a new configuration has been set: core.sshCommand
If this variable is set,
git fetchandgit pushwill use the specified command instead ofsshwhen they need to connect to a remote system.
The command is in the same form as theGIT_SSH_COMMANDenvironment variable and is overridden when the environment variable is set.
It has been introduced in Git 2.10, commit 3c8ede3, June 2016
Since then, you have Git 2.13, commit dd33e077, Feb. 2017, which has ssh.variant
Depending on the value of the environment variables
GIT_SSHorGIT_SSH_COMMAND, or the config settingcore.sshCommand, Git auto-detects whether to adjust its command-line parameters for use with plink or tortoiseplink, as opposed to the default (OpenSSH).The config variable
ssh.variantcan be set to override this auto-detection; valid values aressh,plink,puttyortortoiseplink.
Any other value will be treated as normal ssh. This setting can be overridden via the environment variableGIT_SSH_VARIANT.
For those, I need to use the SSH client that Git for Windows came with
So for the repos where you need ssh instead of putty, you can use both settings to set exactly in your configuration what you want.
cd /path/to/my/repo
git config ssh.variant ssh
GIT_SSH isn't mentioned in the git config man page.
"Git clone with custom SSH using GIT_SSH error" describes how to pass option to GIT_SSH, but illustrates that it isn't part of a git repo config.
None of the answers so far worked for me. What ended up fixing this issue for me was removing quotes from my GIT_SSH variable and don't escape any characters at all, no MSYS path style (eg. /c/path\ to\ putty/plink.exe). Just enter the path normally, Git handles the quoting.
set GIT_SSH=C:\path to putty\plink.exe
That's it. When using GIT_TRACE you can see that the variable gets quoted in the resulting command so:
the added double quotes change the string passed to the command and
the path is wrapped in single quotes so the spaces are ok.
Hope that helps someone.
In my case setting GIT_SSH to:
GIT_SSH=/c/Program\ Files\ (x86)/Git/bin/ssh.exe
worked in git bash.
Edit: This should not be required any more. Instead use the Windows credential manager. Thanks @breakpoint
You don't have to set an environment variable anymore in Windows.
With git 2.10+ (Q3 2016), you also have the possibility to set a config for GIT_SSH_COMMAND, which is easier than an environment variable (and can be set globally, or locally for a specific repo)
See commit 3c8ede3 (26 Jun 2016) by Nguyễn Thái Ngọc Duy (pclouds).
(Merged by Junio C Hamano -- gitster -- in commit dc21164, 19 Jul 2016)
A new configuration variable
core.sshCommandhas been added to specify what value for GIT_SSH_COMMAND to use per repository.
core.sshCommand:
If this variable is set,
git fetchandgit pushwill use the specified command instead ofsshwhen they need to connect to a remote system.
The command is in the same form as theGIT_SSH_COMMANDenvironment variable and is overridden when the environment variable is set.
It means the git push can be:
cd /path/to/my/repo
git config core.sshCommand 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
# later on
git push origin master
With Git 2.16 (Q1 2018), you will have a new mechanism to upgrade the wire protocol in place is proposed and demonstrated that it works with the older versions of Git without harming them.
See commit 6464679 (16 Oct 2017), and commit 0cd8328 (26 Sep 2017) by Jonathan Tan (jhowtan).
See commit 94b8ae5, commit 3c88ebd, commit 19113a2, commit 0c2f0d2, commit 2609043, commit aa9bab2, commit dfe422d, commit 373d70e, commit 5d2124b (16 Oct 2017) by Brandon Williams (mbrandonw).
(Merged by Junio C Hamano -- gitster -- in commit 4c6dad0, 06 Dec 2017)
ssh: introduce a 'simple' ssh variantWhen using the '
ssh' transport, the '-o' option is used to specify an environment variable which should be set on the remote end.
This allows Git to send additional information when contacting the server, requesting the use of a different protocol version via the 'GIT_PROTOCOL' environment variable like so: "-o SendEnv=GIT_PROTOCOL".Unfortunately not all ssh variants support the sending of environment variables to the remote end.
To account for this, only use the '-o' option for ssh variants which are OpenSSH compliant.
This is done by checking that the basename of the ssh command is 'ssh' or the ssh variant is overridden to be 'ssh' (via thessh.variantconfig).Other options like '
-p' and '-P', which are used to specify a specific port to use, or '-4' and '-6', which are used to indicate that IPV4 or IPV6 addresses should be used, may also not be supported by all ssh variants.Currently if an ssh command's basename wasn't '
plink' or 'tortoiseplink', Git assumes that the command is an OpenSSH variant.
Since user configured ssh commands may not be OpenSSH compliant, tighten this constraint and assume a variant of 'simple' if the basename of the command doesn't match the variants known to Git.
The new ssh variant 'simple' will only have the host and command to execute ([username@]hostcommand) passed as parameters to thesshcommand.
Here is the answer:
set GIT_SSH_COMMAND=ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no & git push origin master
None of these solutions worked for me.
Instead, I elaborate on @Martin v. Löwis's mention of setting a config file for SSH.
SSH will look for the user's ~/.ssh/config file. I have mine setup as:
Host gitserv
Hostname remote.server.com
IdentityFile ~/.ssh/id_rsa.github
IdentitiesOnly yes # see NOTES below
AddKeysToAgent yes
And I add a remote git repository:
git remote add origin git@gitserv:myrepo.git
(or clone a fresh copy of the repo with git@gitserv:myrepo.git as address)
And then git commands work normally for me.
git push -v origin master
If you have submodules, you can also execute the following in the repo directory, to force the submodules to use the same key:
git config url.git@gitserv:.insteadOf https://remote.server.com
NOTES
The
IdentitiesOnly yesis required to prevent the SSH default behavior of sending the identity file matching the default filename for each protocol. If you have a file named~/.ssh/id_rsathat will get tried BEFORE your~/.ssh/id_rsa.githubwithout this option.AddKeysToAgent yeslets you avoid reentering the key passphrase every time.You can also add
User gitto avoid writinggit@every time.
References
- Best way to use multiple SSH private keys on one client
- How could I stop ssh offering a wrong key
Something like this should work (suggested by orip):
ssh-agent bash -c 'ssh-add /somewhere/yourkey; git clone [email protected]:user/project.git'
if you prefer subshells, you could try the following (though it is more fragile):
ssh-agent $(ssh-add /somewhere/yourkey; git clone [email protected]:user/project.git)
Git will invoke SSH which will find its agent by environment variable; this will, in turn, have the key loaded.
Alternatively, setting HOME may also do the trick, provided you are willing to setup a directory that contains only a .ssh directory as HOME; this may either contain an identity.pub, or a config file setting IdentityFile.