gitconfig 使用不同的用户名和邮箱

在使用 git 的过程中经常会有这样的场景,公司提交代码要求使用公司的用户名和邮箱,但是自己提交到 github 等则希望使用自己的邮箱。但是 git 又没有提供根据域名配置用户名和邮箱的功能。

使用 gitconfig 的 includeIf 指令。不过这种方式需要自己的仓库按某种固定路径存放。

首先在 gitconfig 下进行如下配置

[includeIf "gitdir:~/go/src/company_code/"]
    path = ~/.gitconfig-company

然后新建 gitconfig-company

[user]
    name=xxx
    email=xxx@company.com

这样 ~/go/src/company_code 目录下的仓库就会使用指定的用户名和邮箱,其余的还是使用全局配置。