場景

使用 git 時,對於公司專案和個人專案想用不同的使用者名稱和郵箱提交,簡單的解決方式就是對 git 倉庫單獨配置 user.nameuser.email

直接修改當前倉庫的 .git/config 配置

[user]
name = zhangsan
email = [email protected]

或是使用命令列

git config user.name zhangsan
git config user.email [email protected]

但如果專案越來越多,每個需要配置的倉庫都要修改一遍,實在是有些麻煩,git 給我們提供了更方便的 solution

解決方式

新建一個 .gitconfig-personal 檔案(檔名取決於你),新增以下內容,此檔案就作為個人的 git 配置

[user]
name = zhangsan
email = [email protected]

修改 .gitconfig 檔案(位置在使用者根目錄),添加個人配置

[includeIf "gitdir:D:/project/personal/"]
path = .gitconfig-personal

儲存後,在 D:/project/personal/ 下新的倉庫都會以 .gitconfig-personal 中的使用者名稱和郵箱提交了

最後執行以下命令檢視是否生效:

git config --show-origin --get user.name
git config --show-origin --get user.email

其他

推薦三個好用的 git 配置:https://spin.atomicobject.com/2020/05/05/git-configurations-default/