如何在一臺主機上管理自己的多個git repository
在使用git時,通常是直接ssh-keygen生成預設祕鑰.然後將共鑰新增到遠端倉庫,就可以訪問了.
但是,當我們有多個repository時,這種方式就不適用了,因為一個祕鑰只能關聯一個遠端倉庫.
如果想同時管理多個repository,這時就需要生成多個祕鑰,然後配置祕鑰和遠端倉庫的關聯.
步驟1.生成指定倉庫的祕鑰
1. ssh-keygen -t rsa -b 4096 -C "[email protected]" 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配置檔案,檔名為configtouch 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 [email protected]:user/repository.git
而變成了
git remote add origin [email protected]: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