因为用git通过SSH协议commit代码到GitHub,不用每次都输入账号和密码。所以非常方便,现在我们就来看看需要怎样做。
一、如果目前需要每次都输入账号和密码,则可以通过执行命令切换成通过SSH协议登录:
(如果还没有设置SSH请先按照下面第二点提示操作)
git remote rm origin
git remote add origin {git SSH Address}
{git SSH Address} 类似这样的:git@github.com:YourAccount/YourProjectName
可以通过以下方式获取。
二、 如何在本地生成SSH key及如何设置GitHub允许通过SSH协议登录。
可参考GitHub官方文档:生成SSH keys (分不同平台Windows/Mac/Linux等) 一下为Mac OSX讲解
-
Open Terminal. 开命令行
-
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.
输入一个文件名字来存储key,可以什么都不输入直接回车存储到默认的.ssh/下
> Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
-
At the prompt, type a secure passphrase. For more information, see "Working with SSH key passphrases".
可以为key输入密码,也可以直接回车不加密
> Enter passphrase (empty for no passphrase): [Type a passphrase] > Enter same passphrase again: [Type passphrase again]
Adding your SSH key to the ssh-agent 将SSH key加入 ssh-agent:
-
Start the ssh-agent in the background.
$ eval "$(ssh-agent -s)" > Agent pid 59566
-
If you're using macOS Sierra 10.12.2 or later, you will need to modify your
~/.ssh/config
file to automatically load keys into the ssh-agent and store passphrases in your keychain.这一步可以忽略
Host * AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_rsa
-
Add your SSH private key to the ssh-agent and store your passphrase in the keychain. 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 -K ~/.ssh/id_rsa
将生成的public key加入GitHub:
即id_rsa.pub加入GitHub。