檢查是否存在SSH密碼
Before you generate an SSH key, you can check to see if you have any existing SSH keys.
Windows
-
Open Git Bash.
-
Enter
ls -al ~/.ssh
to see if existing SSH keys are present:$ ls -al ~/.ssh # Lists the files in your .ssh directory, if they exist
-
Check the directory listing to see if you already have a public SSH key. By default, the filenames of the public keys are one of the following:
- id_rsa.pub
- id_ecdsa.pub
- id_ed25519.pub
If you don't have an existing public and private key pair, or don't wish to use any that are available to connect to GitHub, then generate a new SSH key.
If you see an existing public and private key pair listed (for example id_rsa.pub and id_rsa) that you would like to use to connect to GitHub, you can add your SSH key to the ssh-agent.
Tip: If you receive an error that ~/.ssh doesn't exist, don't worry! We'll create it when we generate a new SSH key.
Linux
-
Open Terminal.
-
Enter
ls -al ~/.ssh
to see if existing SSH keys are present:$ ls -al ~/.ssh # Lists the files in your .ssh directory, if they exist
-
Check the directory listing to see if you already have a public SSH key. By default, the filenames of the public keys are one of the following:
- id_rsa.pub
- id_ecdsa.pub
- id_ed25519.pub
If you don't have an existing public and private key pair, or don't wish to use any that are available to connect to GitHub, then generate a new SSH key.
If you see an existing public and private key pair listed (for example id_rsa.pub and id_rsa) that you would like to use to connect to GitHub, you can add your SSH key to the ssh-agent.
生成新的SSH密鑰
Windows
-
Open Git Bash.
-
Paste the text below, substituting in your GitHub email address.
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This creates a new ssh key, using the provided email as a label.
> Generating public/private rsa key pair.
-
When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location.
> Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]
-
At the prompt, type a secure passphrase. For more information, see "Working with SSH key passphrases".
> Enter passphrase (empty for no passphrase): [Type a passphrase] > Enter same passphrase again: [Type passphrase again]
Linux
-
打開終端。
-
粘貼以下文本,替換為您的GitHub電子郵件地址。
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
使用提供的電子郵件作為標簽,這將創建一個新的ssh密鑰。
> Generating public/private rsa key pair.
-
當提示您“輸入要在其中保存密鑰的文件”時,請按Enter。這接受默認文件位置。
> Enter a file in which to save the key (/home/you/.ssh/id_rsa): [Press enter]
-
在提示符下,鍵入一個安全密碼。有關更多信息,請參閱“使用SSH密鑰密碼短語”。
> Enter passphrase (empty for no passphrase): [Type a passphrase] > Enter same passphrase again: [Type passphrase again]
將SSH密鑰添加到ssh-agent
在將新的SSH密鑰添加到ssh-agent來管理密鑰之前,您應該已經檢查了現有的SSH密鑰並生成了一個新的SSH密鑰。
-
在后台啟動ssh-agent。
$ eval "$(ssh-agent -s)" > Agent pid 59566
-
將SSH私鑰添加到ssh-agent。如果您使用其他名稱創建密鑰,或者要添加具有其他名稱的現有密鑰,請使用私有密鑰文件的名稱替換命令中的id_rsa。
$ ssh-add ~/.ssh/id_rsa
打開gitbash終端
clip <~/.ssh/id_rsa.pub
然后在Key里復制粘貼,就會有key值出現
最后,在git終端下就可以copy我們的git倉庫代碼了
#git clone git@github.com:YourGitHub/YourProject.git
git clone git@github.com:CarryGitHub/Demo1.git