ssh-keygen -t ed25519 -C "work@company.com" -f ~/.ssh/id_ed25519_work
ssh-keygen -t ed25519 -C "personal@gmail.com" -f ~/.ssh/id_ed25519_personal
# 公司 GitLab
Host gitlab.company.com
HostName gitlab.company.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
# 個人 GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
cd ~/projects/company-repo
git config user.name "Work Name"
git config user.email "work@company.com"
cd ~/projects/personal-repo
git config user.name "Personal Name"
git config user.email "personal@gmail.com"
| 誤區 | 事實 |
|---|---|
| 一台電腦只能有一個 user.name/email | **全域設定** (`git config --global`) 只有一組,但**每個專案可獨立覆蓋** (`git config` 不加 `--global`)。 |
| 一個 SSH Key 只能對應一個倉庫 | **一個 SSH Key 對應一個 Git 平台帳號**,該帳號下**所有有權限的倉庫**皆可存取。 |
| Windows 每個用戶只能一組 SSH Key | 同一用戶可建立多組金鑰檔,透過 `config` 指定。 |
| 專案目錄必須在 Git 安裝目錄下 | **專案可放在任何位置**,`git init` 即可建立倉庫,與安裝目錄無關。 |