Why git identity is keep losing with bitbucket rep

2019-09-12 07:32发布

问题:

Whenever I need to git pull or git push repo on bitbucket.org on Win10 dev laptop, almost every time I need to add the identity before I can do that. Here is the ssh -T:

$ ssh -T git@bitbucket.org
Permission denied (publickey). 

Then after eval "$(ssh-agent) and ssh-add ~/.ssh/id_rsa, I can git pull or git push. Next time I need to repeat the process again. Why the identity is keep losing? How can I add the identity once for all?

回答1:

A good start is to understand the steps you are doing and how ssh-agent works or how you can do that better.

SSH-Agent is a program storing your private key in the memory and to save you from typing passphrase for every connection. Your shell remembers the connection to this program using environment variables. So if you open new shell run ssh-agent and close the shell, you lose this connection (Windows does not have this anyhow integrated). Also restarting computer (obviously) stops the running agent.

But you might use

$ ssh -T -i /path/to/id_rsa git@bitbucket.org

which will do the same (you will have to insert passphrase if you have one). Or more cleanly using ssh_config in ~/.ssh/config, which can contain:

Host bitbucket.org
  User git
  IdentityFile /path/to/id_rsa

and then connecting using

$ ssh -T git@bitbucket.org

will work too.