背景

苦于每个项目都要重新配置一遍用户名和邮箱,一不小心就把自己 Github 的用户信息推到公司的远仓上了,或者反过来。


解决方案

Git 2.13.0及以上版本提供了一 includeIf配置

方案一

使 includeIf匹配文件夹路径,然后指定生效的用户信息文件

我这里就不展示代码了 (偷懒),网上一大堆,而且我觉得这种方案还不如我自己每个项 git config一下来的快

方案二

使 includeIf匹配 Git 的远仓地址,然后指定生效的用户信息文件

C:\Users\your_user_name\.ssh\config

Host gitlab.com
HostName gitlab.com
IdentityFile C:\Users\your_user_name\.ssh\id_rsa_gitlab
PreferredAuthentications publickey
User your_repository_name

Host github.com
HostName github.com
IdentityFile C:\Users\your_user_name\.ssh\id_rsa_github
PreferredAuthentications publickey
User your_repository_name

C:\Users\your_user_name\.gitconfig

[filter "lfs"]
	clean = git-lfs clean -- %f
	smudge = git-lfs smudge -- %f
	process = git-lfs filter-process
	required = true

[includeIf "hasconfig:remote.*.url:git@github.com:*/**"]
	path = .gitconfig_github

[includeIf "hasconfig:remote.*.url:https://github.com/**"]
	path = .gitconfig_github

[includeIf "hasconfig:remote.*.url:git@gitlab.com:*/**"]
	path = .gitconfig_gitlab

注意:
●这里不能直接配置用户信息,要通过 path 指定配置文件
●匹配地址注 *号数量

C:\Users\your_user_name\.gitconfig_xxx

[http]
  proxy = 127.0.0.1:10809
[user]
  name = your_name
  email = your_email