Have create ssh key using following link (https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/) Once create ssh key using git bash and added to github. It works only for that particular git bash. If I close git bash and open again. Same ssh key wont work, need to create new. CAn anyone please tell me why is it happening? or what is missing?
-
1. Which OS are you using? I suppose windows, right? 2. Did you run the command `ssh-add ~/.ssh/id_rsa` right before everything worked for you? 3. Did you try running `ssh-add ~/.ssh/id_rsa` after you reopen git bash and before you tried to call other commands? – Victor Yarema Jan 15 '20 at 19:42
-
Please note that the page https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent recommends to start `ssh-agent` using command `eval $(ssh-agent -s)` which is neither secure nor recommended in general. In order to get familiar with `ssh-agent` I would propose you to read these articles: 1 - http://rabexc.org/posts/pitfalls-of-ssh-agents , 2 - https://docstore.mik.ua/orelly/networking_2ndEd/ssh/ch02_05.htm . – Victor Yarema Jan 15 '20 at 19:48
4 Answers
You would need to auto add the ssh key to every session when you open Git Bash. For that, if you are on Windows, follow the below steps :
- Go to the location of Git installation (usually at
C:\Program Files\Git\etc\ssh
) - Edit the
ssh_config
file and add the lineIdentityFile Drive:\path\to\key
whereDrive:\path\to\key
should specify the local path to your key that you have generated earlier, and save the file after editing.
Now every time you open Git Bash, the key would automatically be added to the ssh session and you will not need to add the ssh key everytime.

- 13,559
- 2
- 38
- 54
No matter what operating system version you run you need to run this command to complete setup:
$ ssh-add -K ~/.ssh/id_rsa

- 101
- 1
- 6
-
11. Could you please explain your answer? 2. I was unable to find the key `-K` (uppercase) in `ssh-add` manual. Did you mean `-k` (lowercase)? – Victor Yarema Jan 15 '20 at 19:38
-
No its uppercase K. Here's what `man ssh-add` shows me `-K: When adding identities, each passphrase will also be stored in the user's keychain.` – harshp Jan 15 '20 at 22:55
-
-
This may be dependent on the OS and ssh version, here's what I have: `➜ ~ sw_vers ProductName: Mac OS X ProductVersion: 10.15.2 BuildVersion: 19C57 ➜ ~ /usr/bin/ssh -V OpenSSH_7.9p1, LibreSSL 2.7.3` – harshp Mar 05 '20 at 07:28
You can follow the below steps if you have to add the key each time you start a new bash session.
- After generating the key and adding it to the .ssh/ folder(if you have doubt in generating the public and private keys you can refer to this link from git documentation [key-gen]).
- The Bash profile is a file on your computer that Bash runs every time a new Bash session is created hence you can start and add the key everytime by using this bash profile in your system.
- Edit .bash_profile of your git bash using the below script. Add this to your .bash_profile
#!/bin/bash
eval `ssh-agent -s`
a=$?
if [ $a == 0 ]
then
ssh-add ~/.ssh/"<yoursshkeyfile>"
b=$?
if [ $b == 0 ]
then
echo "------------------key added-----------------"
else
echo "--------------key not added but agent started----------"
fi
else
echo "----------------agent not started---------------"
fi
This basically starts and adds your key everytime you open the bash.
eval ssh-agent -s
starts the agent and ssh-add ~/.ssh/"<yoursshkeyfile>"
(add the private key file and not the public) adds your key present in ./ssh directory. "$?" this is just to check the exit status of the command executed before. This returns 0 if executed without error.

- 3
- 4
In your ~/.ssh/config
add:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
create the file if it doesn't exist.

- 81
- 1
- 6