How do I tell Git for Windows where to find my private RSA key?

Solution 1:

For Git Bash

If you are running msysgit (I am assuming you are) and are looking to run Git Bash (I recommend it over TortoiseGit, but I lean to the CLI more than GUI now), you need to figure out what your home directory is for Git Bash by starting it then type pwd (On Windows 7, it will be something like C:\Users\phsr I think). While you're in Git Bash, you should mkdir .ssh.

After you have the home directory, and a .ssh folder under that, you want to open PuTTYgen and open the key (.ppk file) you have previously created. Once your key is open, you want to select Conversions -> Export OpenSSH key and save it to HOME\.ssh\id_rsa. After you have the key at that location, Git Bash will recognize the key and use it.

Note: Comments indicate that this doesn't work in all cases. You may need to copy the OpenSSH key to Program Files\Git\.ssh\id_rsa (or Program Files (x86)\Git\.ssh\id_rsa).

For TortoiseGit

When using TortoiseGit, you need to set the SSH key via pacey's directions. You need to do that for every repository you are using TortoiseGit with.

Solution 2:

Using the built-in SSH client shipped with Git for Windows, you need to set up the HOME environment variable so that the Git SSH client can find the key.

For example, on a Windows Vista installation, this would be done by issuing setx HOME c:\Users\admin\ on the command line.

It made my day and fixed the issue with Git provided that your private key is not password protected. If you want to use ssh-agent, then you can probably run ssh-agent cmd.exe (although I've never done that) and the ssh-add as usual.

Note that all Git/SSH tools are supposed to be run from a cmd.exe in order not to blink a window.

If this does not work correctly, using plink can probably be achieved by tweaking GIT_SSH. Refer to all the SVN + ssh tutorials; this is basically the same plumbing you need to setup.


Solution 3:

You can specify the key location for TortoiseGit the following way:

  • Open an Explorer Window.
  • Open the Contextmenu and navigate TortoiseGitSettings
  • In the now opened window, navigate to GitRemote
  • Set the path to your PuTTY key in the corresponding input box.

A screenshot is below:

Enter image description here


Solution 4:

None of the previous answers worked for me. Here was what worked for me in the end. It is actually fairly simple, if you know what to type. It doesn't need PuTTY.

  • Open a Git Bash prompt
  • Type 'ssh-keygen'
    • Accept the default location
    • Choose a blank passphrase (so just press 'enter' to all questions')
  • Now copy the public key to your server, for example: scp ~/.ssh/id_rsa.pub [email protected]:~

That's the bit on your own computer done. Now ssh into the destination server, then do

mkdir -p ~/.ssh
cd ~/.ssh
cat ../id_rsa.pub >> authorized_keys
rm ../id_rsa.pub

That's it! You're done! From Git Bash, do the following to test:

ssh [email protected] ls

If it lists the files in your home directory on the Git server, and then you're done!

For GitHub, you don't have shell access to their server, but you can upload the key using their website, so for the bit 'now copy to your server', do:

  • In Git Bash, type 'cat ~/.ssh/id_rsa.pub', select the result, and copy it to the clipboard.
  • On the GitHub website, go to 'Account settings', 'SSH and GPG keys', click 'New SSH key', and paste the key.

Solution 5:

If you're using msysgit with the OpenSSH tools, you need to either create ~/.ssh/id_rsa, or create a Git configuration in ~/.ssh/config which points to your key.

Here's an example of a Git configuration for Bitbucket that will use the correct username, and a key other than the default key (in case you maintain one key for SSH connections, and another for Git accounts).

~/.ssh/config:

Host bitbucket.org
    Hostname bitbucket.org
    User git
    IdentityFile /C/keys/yourkey.key

Once in Git Bash, you can run two commands to add your key to your current session's ssh-agent to avoid having to repeatedly type the key's password.

eval `ssh-agent`
ssh-add /C/keys/yourkey.key