zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Git config with directory scope, containing multiple repositories

Git with config Directory multiple Scope
2023-09-11 14:14:17 时间

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