Windows 上讓 git 依資料夾自動切換身分
同一台電腦上同時有公司與個人兩個 GitHub 帳號時,很容易用個人信箱提交公司的 commit,或是用錯帳號推送。本文的做法是讓 git 依 repo 所在的資料夾,自動套用對應的身分。設定完成後,你在公司資料夾裡和資料夾外的行為如下:
| 項目 | 公司資料夾內的 repo | 公司資料夾外的 repo |
|---|---|---|
| commit 的信箱 | company@example.com | personal@example.com |
| 使用的 GitHub 帳號 | company-account | personal-account |
git 的身分其實是兩件獨立的事:
- 提交身分(commit 上記錄的名字與信箱,由
user.name與user.email決定):由 git 本體的includeIf處理,純本機設定,與有沒有登入無關。這是 git 內建的功能,行為明確,可以用指令驗證。 - 連線憑證(push、fetch 時用哪個 GitHub 帳號驗證):由 Git Credential Manager(GCM,隨 Git for Windows 安裝)處理。它是另一個工具,行為有幾個需要留意的地方,所以要和第一件事分開看。
設定
includeIf 在條件成立時,才會載入另一份設定檔,載入的值會蓋過全域的值,git 2.13 起支援。以下的資料夾、信箱與帳號都是示範值,請換成自己的。
第一個檔案是全域設定 C:\Users\you\.gitconfig,其中 [credential] 必須在 [includeIf] 之前:
[user]
name = Your Name
email = personal@example.com
[credential "https://github.com"]
username = personal-account
[includeIf "gitdir/i:C:/work/"]
path = C:/Users/you/.gitconfig-work
第二個檔案是公司設定檔 C:\Users\you\.gitconfig-work,由上面的 includeIf 在公司資料夾內載入:
[user]
name = Your Name
email = company@example.com
[credential "https://github.com"]
username = company-account
第一次在公司資料夾內的 repo 推送時,GCM 會開啟瀏覽器,用公司帳號登入。
驗證
下面這個指令確認 commit 信箱與它的來源檔案,要在 repo 內執行。公司資料夾內應該看到來源是公司設定檔,資料夾外應該看到來源是全域 .gitconfig:
git config --show-scope --show-origin user.email
- 公司資料夾內:
global<TAB>file:C:/Users/you/.gitconfig-work<TAB>company@example.com - 資料夾外:來源是
.gitconfig,值是personal@example.com - 被 include 的檔案,scope 也會顯示
global,這是正常的。
下面這個指令只讀設定,不會連線,確認 git 會對 GitHub 使用哪個帳號名稱。公司資料夾內應輸出 company-account,資料夾外應輸出 personal-account:
git config --get-urlmatch credential.username https://github.com
最後在公司資料夾內的 repo 執行一次 git fetch。如果能順利完成,而且沒有跳出選擇帳號的視窗,就代表登入也正確。
規則
gitdir/i:路徑比對不分大小寫,Windows 需要這個寫法。- 路徑用正斜線,結尾加
/表示包含底下所有子資料夾,例如C:/work/。 [credential]要寫在[includeIf]之前:同一個設定後讀到的會勝出,而includeIf的內容是在它所在的位置展開。順序反了,公司資料夾內也會用個人帳號。- 全域也要指定個人帳號:登入第二個帳號後,資料夾外若沒有指定帳號,GCM 無法自動選。
- 用
git config --global指令新增的區段會附加在檔尾,順序可能因此相反;直接編輯檔案調整區段順序即可。
限制與坑
- 條件看的是 repo 所在的位置,不是目前所在的資料夾;不在任何 repo 內時不會套用。
- repo 自己的
.git/config若設了user.email,會蓋過這套設定。 - 已經存在的 commit 不會改變。
- GitHub CLI(
gh)與 IDE 內建的 GitHub 登入是另外的登入,不吃這套設定。 gh auth login對「Authenticate Git with your GitHub credentials?」回答 Yes 時,觀察到 git 的登入會跟著 gh 一起登入與失效;想讓兩者互不影響,可以回答 No,這個做法未驗證。
替代做法
如果你的 remote 是 SSH 網址,或不想依賴 GCM 的帳號選擇,可以每個帳號用一把 SSH 金鑰。IdentitiesOnly=yes 讓 ssh 只使用指定的金鑰,不把 ssh-agent 裡的其他金鑰也送給 GitHub。連線到 GitHub 的部分沒有實際驗證。
# .gitconfig-work
[core]
sshCommand = ssh -i ~/.ssh/id_work -o IdentitiesOnly=yes
如果既有的 remote 是 https,又想改走 SSH,可以用 url.<base>.insteadOf 改寫網址。clone 時這個改寫是否已經生效沒有驗證,clone 請直接用 SSH 網址。
[url "git@github.com:"]
insteadOf = https://github.com/
如果公司的 repo 不集中在同一個資料夾,可以改成依 remote 網址切換,需要 git 2.36 以上。repo 放在哪裡都可以,但沒有 remote 就不會套用,被 include 的檔案也不可以再含有 remote 網址。在本機測試時,沒有 remote 不套用、remote 符合時套用、把 remote 改成其他網址後恢復為個人設定;clone 過程中是否套用沒有驗證。
[includeIf "hasconfig:remote.*.url:https://github.com/work-org/**"]
path = C:/Users/you/.gitconfig-work
還原
先備份全域設定,再移除兩項設定並刪除公司設定檔:
cp ~/.gitconfig ~/.gitconfig.bak
git config --global --unset-all "includeIf.gitdir/i:C:/work/.path"
git config --global --unset-all credential.https://github.com.username
rm ~/.gitconfig-work
參考資料
- https://git-scm.com/docs/git-config
- https://github.com/git/git/blob/master/Documentation/RelNotes/2.13.0.adoc
- https://github.com/git/git/blob/master/Documentation/RelNotes/2.26.0.adoc
- https://github.com/git/git/blob/master/Documentation/RelNotes/2.36.0.adoc
- https://github.com/git-ecosystem/git-credential-manager/blob/main/docs/multiple-users.md
- https://docs.github.com/en/account-and-profile/how-tos/account-management/managing-multiple-accounts
沒有留言:
張貼留言