# 前言
相信在同一機器上部署多個應用並不是什麼罕見的需求,特別是像我這樣很小的個人網站來說更是如此。但在我部署子站的時候發現一個問題,那就是 Github 並不允許多個倉庫使用相同的 Deploy Key。如果你嘗試這樣做的話,會得到如下提示:
# 建立 ssh keygen
首先,在生成 SSH Key 的時候,需要添加 -f 參數,指定不同於默認的文件名(id_rsa),以免無意中覆蓋:
在回應內容,我們都不設定任何密碼,直接按 enter 或 y
命令行提示符 | | ssh-keygen -t rsa -f ~/.ssh/repo1_rsa -C "Github email" |
| | Generating public/private rsa key pair. |
| | Enter file in which to save the key (/Users/使用者名稱/.ssh/id_rsa): |
| | /Users/使用者名稱/.ssh/id_rsa already exists. |
| | Overwrite (y/n)? y |
| | Enter passphrase (empty for no passphrase): |
| | Enter same passphrase again: |
| | Your identification has been saved in /Users/使用者名稱/.ssh/id_rsa. |
| | Your public key has been saved in /Users/使用者名稱/.ssh/id_rsa.pub. |
| | The key fingerprint is: |
| | SHA256:3Cor2z.............d3h31 your_email@example.com |
| | The key's randomart image is: |
| | +---[RSA 4096]----+ |
| | |=.. .o | |
| | |... |
| | |+X+*.. | |
| | +----[SHA256]-----+ |
| | li |
命令行提示符 | | ssh-keygen -t rsa -f ~/.ssh/repo2_rsa -C "Github email" |
| | Generating public/private rsa key pair. |
| | Enter file in which to save the key (/Users/使用者名稱/.ssh/id_rsa): |
| | /Users/使用者名稱/.ssh/id_rsa already exists. |
| | Overwrite (y/n)? y |
| | Enter passphrase (empty for no passphrase): |
| | Enter same passphrase again: |
| | Your identification has been saved in /Users/使用者名稱/.ssh/id_rsa. |
| | Your public key has been saved in /Users/使用者名稱/.ssh/id_rsa.pub. |
| | The key fingerprint is: |
| | SHA256:3Cor2z.............d3h31 your_email@example.com |
| | The key's randomart image is: |
| | +---[RSA 4096]----+ |
| | |=.. .o | |
| | |... |
| | |+X+*.. | |
| | +----[SHA256]-----+ |
| | li |
# 將 SSH Key 加到 ssh-agent
# 將 SSH Private Key 加到 ssh-agent
# 複製 SSH Key
# 前往 Github Deploy Key 區
前往 Repository 進入專案後,點擊 settings
點選左區選單 Deploy Key ,接著按右邊上角的 Add deploy key
貼上剛剛複製的 SSH Key 以及輸入標題,儲存
# 編輯 ~/.ssh/config
文件以指定不同的登入配置
命令行提示符 | | vim ~/.ssh/config |
| | a |
| | |
| | Host repo1.github.com |
| | Hostname github.com |
| | User git |
| | IdentityFile ~/.ssh/repo1_rsa |
| | IdentitiesOnly yes |
| | |
| | Host repo2.github.com |
| | Hostname github.com |
| | User git |
| | IdentityFile ~/.ssh/repo2_rsa |
| | IdentitiesOnly yes |
| | |
| | :wq |
這裡的要點是,用不同的 Host 代替默認的 git 協議地址(github.com)。
然後,你可以按照配置,把生成的 SSH Key 配置到不同的 Github 倉庫。
# 改 SSH URL
命令行提示符 | | git@repo1.github.com:user/[project].git |
命令行提示符 | | git@repo2.github.com:user/[project].git |
對於已經 clone 的項目,建議刪掉.git 資料夾重新 pull ㄅ