在使用git时,一般是直接ssh-keygen生成默认秘钥.而后将共钥添加到远程仓库,就能够访问了.git
可是,当咱们有多个repository时,这种方式就不适用了,由于一个秘钥只能关联一个远程仓库.github
若是想同时管理多个repository,这时就须要生成多个秘钥,而后配置秘钥和远程仓库的关联.ssh
1. ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
2. Enter a file in which to save the key (/home/you/.ssh/id_rsa): new_repository 这里再也不使用默认,而是须要指定ssh key的名字,用来和指定仓库关联
3. 最后输入密码时,通常直接回车便可.
1. 进入用户目录的.ssh文件夹中 cd ~/.ssh
2. 建立ssh配置文件,文件名为config touch config
3. 进入config配置
#默认配置
Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa
#新仓库指定配置 Host new_repository.github.com HostName github.com User git IdentityFile ~/.ssh/new_repository
上面的两个配置中,值得注意的是Host不一样,第二的Host添加指定前缀(通常为repository的名字)测试
添加前缀后,咱们为本地repository添加远程链接时就不是spa
git remote add origin git@github.com:user/repository.git
而变成了
git remote add origin git@xxx.github.com:user/repository.git
此配置就是为指定仓库添加别名,好为不一样仓库指定不一样的秘钥文件
1. 首先进入.ssh文件,复制new_repository.pub内容code
2. 进入repository settings,在以下页面点击add deploy key 粘贴new_repository.pubblog
git init git add README.md git commit -m "first commit" git push -u origin master