如何在一台主机上管理自己的多个git repository

在使用git时,通常是直接ssh-keygen生成默认秘钥.然后将共钥添加到远程仓库,就可以访问了.

但是,当我们有多个repository时,这种方式就不适用了,因为一个秘钥只能关联一个远程仓库.

如果想同时管理多个repository,这时就需要生成多个秘钥,然后配置秘钥和远程仓库的关联.

步骤1.生成指定仓库的秘钥

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. 最后输入密码时,一般直接回车即可.

步骤2.为指定仓库设置秘钥配置

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

步骤3.使用配置的连接别名添加远程仓库

上面的两个配置中,值得注意的是Host不同,第二的Host添加指定前缀(一般为repository的名字)

添加前缀后,我们为本地repository添加远程连接时就不是

git remote add origin git@github.com:user/repository.git
而变成了
git remote add origin git@xxx.github.com:user/repository.git

此配置就是为指定仓库添加别名,好为不同仓库指定不同的秘钥文件

步骤4. 在远程仓库添加deploy_key

1. 首先进入.ssh文件,复制new_repository.pub内容

2. 进入repository settings,在如下页面点击add deploy key 粘贴new_repository.pub

步骤5.测试连接

git init
git add README.md
git commit -m "first commit"
git push -u origin master

 

 
posted @ 2018-12-08 12:44  宇的季节  阅读(498)  评论(0编辑  收藏  举报