Git:为文件夹内的所有子文件夹设置共有配置

828 阅读1分钟

主要解决问题:区分个人项目与公司项目的仓库配置,为文件夹内所有子文件夹单独设置配置。

使用个人电脑同时为公司、个人项目提交代码时,commit message 中会包含自己的 email 等信息。一般来说,公司 email 与个人 email 是要区分开的。在我们初次学习 git 时,教程中都会这么指导:

全局仓库配置

git config --global user.name "ubbcou"
git config --global user.email "ubbcou@outlook.com"

当前仓库的配置

git config --local user.name "temp-name"
git config --local user.email "my@temp.com"

这样我们就学习到了为每个仓库设置独属于它的署名配置。

但是如果我们想不破坏全局仓库配置的情况下,为几个甚至几十个公司仓库配置呢,难道每一个仓库都要去使用 git config --local user.name "temp-name",这显然是不合理的。

这时候我们要使用 conditional includes (git version 2.13)的概念,其中有个 includeIf 语法,有条件的使用不同的配置。

食用方式:

# ~/.gitconfig
[user]
  name = ubbcou
  email = ubbcou@outlook.com
[includeIf "gitdir:~/workspace/"] # 带盘符的文件夹路径 "gitdir:C:/dev/workspace/"
  path = .gitconfig-job
# ~/.gitconfig-job
[user]
  name = job name
  email = job@job.com

这份配置的结果是:~/workspace/ 目录下会将 .gitconfig-job 配置内容包含进来(将默认配置覆盖),而以外的正常使用默认配置。

参考: - Is it possible to have different Git configuration for different projects?