Git config with directory scope, containing multiple repositories

Git config with directory scope, containing multiple repositories

based on https://stackoverflow.com/a/44036640/2377961 i think I found a way how it could work.

first you create different config files for your "custom-scopes" (e.g. professional, freetime, ect.) and add your desired user-config for each scope

# ~/all-projects.gitconfig
[user]
   name = TheOperator

.

# ~/professional.gitconfig
[user]
   email = yourname@yourwork.com

.

# ~/freetime.gitconfig
[user]
   email = yourname@private-email.com

than you add lines like this in your standard gitconfig:

# ~/.gitconfig
[include]
    path = all-projects.gitconfig
[includeIf "gitdir/i:c:/work/"]
    path = professional.gitconfig
[includeIf "gitdir/i:c:/freetime/"]
    path = freetime.gitconfig 

The directories after "gitdir/i" should match the parents directory of your project groups. In my example you should store your git-repos for freetime-domains e.g. "c:/freetime/my-freetime.domain/.git"

 

Can I specify multiple users for myself in .gitconfig?

With conditional includes in Git 2.13, it is now possible to have multiple user/email coexist on one machine with little work.

user.gitconfig has my personal name and email. work-user.gitconfig has my work name and email. Both files are at ~ path.

So my personal name/email applies by default. For c:/work/ dir, my work name/email is applied. For c:/work/github/ dir, my personal name/email is applied. This works as the last setting gets applied.

# ~/.gitconfig
[include]
    path = user.gitconfig
[includeIf "gitdir/i:c:/work/"]
    path = work-user.gitconfig
[includeIf "gitdir/i:c:/work/github/"]
    path = user.gitconfig

gitdir is case-sensitive and gitdir/i is case-insensitive.

"gitdir/i:github/" would apply the conditional include for any directory with github in its path.

 

Git 2.13 conditional config on windows

Your global C:/Users/<user-name>/.gitconfig should have this includeIf:

[includeIf "gitdir:C:/Users/<user-name>/Documents/webstorm/corporate/"]
    path = .gitconfig-work

with having your work Git repos in C:/Users/<user-name>/Documents/webstorm/corporate and the conditional work configuration should be located at C:/Users/<user-name>/.gitconfig-work.

That's at least working for me in Window's cmd and Cmder. A git config --show-origin --get user.email should than show you from where a config value is loaded/resolved.

It also seems like the conditional work configuration is only used when issued from within a Git repository.

C:\Users\<user-name>\Documents\webstorm\corporate
λ git config --show-origin --get user.email
file:C:/Users/<user-name>/.gitconfig  foo@oss.com

C:\Users\<user-name>\Documents\webstorm\corporate\some-repo
λ git config --show-origin --get user.email
file:C:/Users/<user-name>/.gitconfig-work  foo@company.com

C:\Users\<user-name>\Documents\webstorm\corporate\some-non-repo-dir
λ git config --show-origin --get user.email
file:C:/Users/<user-name>/.gitconfig  foo@oss.com

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(42)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2019-09-01 ailoop2里面的1个待考察的,在ailoop3里面的操作。(先使用海巨人,不使用英雄技能召唤图腾的问题)
2017-09-01 学习bootstrap
2015-09-01 检查string是否为double
2015-09-01 How to hide TabPage from TabControl
2014-09-01 win7下配置IIS
点击右上角即可分享
微信分享提示