40

I have an SSH key saved in D:/keys folder. I want to add it to my git bash. All the tutorials I found is how to generate SSH key using gitbash and load it to github/gitlab. I generated my SSH key using puttygen. Now I want to add it to my git bash so that I can clone a repository from remote. How can I do that?

4 Answers 4

62

On windows you might need to start the ssh agent like this

# start the ssh-agent in the background
$ eval $(ssh-agent -s)
> Agent pid 59566

Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_rsa in the command with the name of your private key file.

$ ssh-add <path/to/key>

Got this information from here under "Adding your SSH key to the ssh-agent": https://help.github.com/en/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#adding-your-ssh-key-to-the-ssh-agent

Sign up to request clarification or add additional context in comments.

4 Comments

it works but how to make this permanent? I have to repeat each time I restart git bash
@Phate, Did you manage to understand how to add this permanently and repeat for every restart
One way I guess would be to add it to the .bashrc file?
@Phate This answer helped: stackoverflow.com/questions/46661525/permanently-add-an-ssh-key The idea is to create (or update) a file named 'config' in your .ssh folder with, for instance for bitbucket, the following lines adapted to your needs: Host bitbucket.org User yourbitbucketusername IdentityFile ~/.ssh/yourkey
12

Assume the private key file you want to import to git bash is D:/keys folder/myprivatekey and your Git was installed in D:/Git (in which folder you would see the binary file git-bash.exe), open the file D:/Git/etc/ssh/ssh_config.

Here are some texts in this file:

...
# StrictHostKeyChecking ask
# IdentityFile ~/.ssh/id_rsa
# IdentityFile ~/.ssh/id_dsa
# IdentityFile ~/.ssh/id_ecdsa
# IdentityFile ~/.ssh/id_ed25519
# Port 22
...

Simply add a new line and save it:

...
# StrictHostKeyChecking ask
IdentityFile "D:/keys folder/myprivatekey"
# IdentityFile ~/.ssh/id_rsa
# IdentityFile ~/.ssh/id_dsa
# IdentityFile ~/.ssh/id_ecdsa
# IdentityFile ~/.ssh/id_ed25519
# Port 22
...

And the key is already added.

3 Comments

In my case, Windows 11, Git Bash is installed under the Program Files folder and the file I was looking for is at C:\Program Files\Git\etc\ssh\ssh_config. Also, remember run GIt Bash as administrator, otherwise you will get permission restrictions.
you dont need to change the config file with this method, only put the generated private keyfile to the %USERPROFILE%\.ssh\id_rsa or other type
This should be the accepted answer in my opinion as this makes it permanent.
10

I don't think there is any specific config in gitbash itself. You have to put the key in the default location ~\.ssh/id_rsa and it will be used. If you need to have it somewhere else you could do so with a config file same as on Linux ~/.ssh/config

host example.com
 HostName example.com
 IdentityFile ~/.ssh/id_rsa
 User git

Don't forget to set the permissions chmod 400 ~/.ssh/id_rsa

1 Comment

windows ~ equivalent is %USERPROFILE%, so it will be %USERPROFILE%\.ssh\id_rsa
1

I was able to get it so the passphrase is only prompted for on the first window that's opened after booting using the script at Auto-launching ssh-agent on Git for Windows. I did find, however, it didn't work when I added it add it to either ~/.profile or ~/.bashrc. I needed to add it to ~/.bash_profile for it to get picked up and used by Git Bash on Windows.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.