同一台 Mac 管理多個 GitLab 帳號
在同一台 Mac 上同時使用兩個(或以上)GitLab 帳號(例如公司與個人)時,需要解決兩件事:
- SSH 驗證:連到 GitLab 時用哪一把金鑰(對應哪個帳號)
- Git 身分:commit 時使用的
user.name/user.email(避免 commit 作者混亂)
做法可分兩種:僅用 SSH config + 每專案手動設 user,或 SSH config + .gitconfig 條件式設定(建議)。
核心概念
- 不同 Host 名稱 → 不同 SSH Key:在
~/.ssh/config裡為同一個gitlab.com設定多個「虛擬 Host」(如gitlab.com-personal、gitlab.com-work),各自指定不同的IdentityFile。 - Clone / remote URL:clone 或設定 remote 時改用虛擬 Host,例如
git@gitlab.com-work:group/project.git,SSH 就會自動用對應的金鑰。 - 建議:用 .gitconfig 的
includeIf依「目錄」或「remote URL」自動帶入對應的user.name/user.email,不需每個 repo 手動設一次。
共通步驟 1:為不同帳號產生獨立 SSH Key
為每個帳號各產生一對金鑰,檔名區分清楚。
# 個人帳號
ssh-keygen -t ed25519 -C "personal@email.com" -f ~/.ssh/id_ed25519_personal
# 公司帳號
ssh-keygen -t ed25519 -C "work@company.com" -f ~/.ssh/id_ed25519_work
若詢問 passphrase,可直接 Enter 跳過。
共通步驟 2:把公鑰加到對應的 GitLab 帳號
- 複製公鑰:
pbcopy < ~/.ssh/id_ed25519_personal.pub(個人)、pbcopy < ~/.ssh/id_ed25519_work.pub(公司) - 登入各 GitLab 帳號,Settings → SSH Keys,貼上並儲存。
共通步驟 3:設定 SSH Config
讓 SSH 依「虛擬 Host」選擇金鑰。編輯或新增 ~/.ssh/config:
nano ~/.ssh/config
內容範例:
# 個人 GitLab
Host gitlab.com-personal
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
# 公司 GitLab
Host gitlab.com-work
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_work
之後 clone / remote 都要用上述 Host 名稱(見下方「使用方式」)。
作法 A:僅 SSH config + 每專案手動設 user(不建議)
- Clone:改用虛擬 Host
- 個人:
git clone git@gitlab.com-personal:username/project.git - 公司:
git clone git@gitlab.com-work:company/project.git
- 個人:
- 已存在的專案:改 remote
git remote set-url origin git@gitlab.com-personal:username/project.git - 身分:每個專案目錄下手動設一次,否則會用到全域的 name/email:
cd /path/to/personal_project
git config user.name "Your Name"
git config user.email "personal@email.com"
缺點:每個 repo 都要記得設一次,容易漏設或設錯。
作法 B:SSH config + .gitconfig 條件式設定(建議)
在維持上述 SSH config 的前提下,用 Git 的 includeIf 依「目錄」或「remote URL」自動載入對應的 user.name / user.email,不需每個 repo 手動設定。
B-1:依「目錄」區分(gitdir)
適合把公司專案都放在某個目錄(如 ~/work/)、個人專案放在另一目錄(如 ~/personal/ 或 ~/Projects/)。
1. 主設定檔 ~/.gitconfig
[user]
name = Your Name
email = personal@email.com
# 在 work 目錄下使用公司身分
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
# 在 personal 目錄下使用個人身分(可選,若與預設相同可省略)
[includeIf "gitdir:~/personal/"]
path = ~/.gitconfig-personal
2. 公司用設定檔 ~/.gitconfig-work
[user]
name = Your Name
email = work@company.com
3. 個人用設定檔 ~/.gitconfig-personal(可選)
[user]
name = Your Name
email = personal@email.com
gitdir:路徑結尾的/表示「該目錄及其子目錄」都會套用;沒有/只會套用該精確路徑。
B-2:依「remote URL」區分(hasconfig)
不想依目錄、想依「這個 repo 的 remote 是哪個 GitLab」來決定身分時,可用 hasconfig:remote.*.url。
因我們用虛擬 Host,URL 會是 git@gitlab.com-work:... 或 git@gitlab.com-personal:...,可寫成:
主設定檔 ~/.gitconfig
[user]
name = Your Name
email = personal@email.com
[includeIf "hasconfig:remote.*.url:git@gitlab.com-work:**"]
path = ~/.gitconfig-work
[includeIf "hasconfig:remote.*.url:git@gitlab.com-personal:**"]
path = ~/.gitconfig-personal
~/.gitconfig-work、~/.gitconfig-personal 內容同上,只放對應的 [user] 即可。
這樣只要 remote URL 用對(gitlab.com-work / gitlab.com-personal),commit 時就會自動用對應的 name/email,與專案放在哪個目錄無關。
使用方式整理(兩種作法皆同)
| 情境 | 個人帳號 | 公司帳號 |
|---|---|---|
| Clone | git clone git@gitlab.com-personal:username/project.git | git clone git@gitlab.com-work:company/project.git |
| 改既有專案 remote | git remote set-url origin git@gitlab.com-personal:username/project.git | git remote set-url origin git@gitlab.com-work:company/project.git |
SSH 會依 Host 名稱(gitlab.com-personal / gitlab.com-work)自動選對金鑰;若採用作法 B,Git 會依目錄或 remote URL 自動選對 user/email。
驗證設定
SSH 連線(把 gitlab.com-work 換成你要測的 Host):
ssh -T git@gitlab.com-work
成功時會看到 GitLab 的歡迎訊息(含該帳號的 username)。
目前生效的 user 設定(在專案目錄下執行):
git config --show-origin user.email
git config --show-origin user.name
可確認是從哪個 config 檔案讀取,以及數值是否正確。
流程整理
- 為各帳號產生獨立 SSH key(檔名區分)。
- 將公鑰分別加到對應 GitLab 帳號的 SSH Keys。
- 在
~/.ssh/config設定虛擬 Host 與對應IdentityFile。 - Clone / remote 一律使用虛擬 Host(
gitlab.com-personal/gitlab.com-work)。 - 建議:在
~/.gitconfig用includeIf(gitdir 或 hasconfig)載入各身分的 config,避免每專案手動設 user。