What is the proper way to have multiple .gitconfig files for different platforms?

132 Views Asked by At

I have a default global ~/.gitconfig file in my HOMEDIR with the following:

[includeIf "gitdir:/mnt/c/Users/myname/Documents/work/github"]
  path = /mnt/c/Users/myname/.gitconfig-github

[includeIf "gitdir:/mnt/c/Users/myname/Documents/work/gitlab"]
  path = /mnt/c/Users/myname/.gitconfig-gitlab

[init]
        defaultBranch = main

I have 2 more config files that are referenced above in the global .gitconfig, these are in my main working locations (not HOMEDIR).

Here is one example, of the .gitconfig-github file:

[gpg]
    format = ssh
[commit]
    gpgsign = true
[user]
    name = mygithubname
    email = [email protected]
    signingkey = /mnt/c/Users/myname/.ssh/id_rsa.pub

When I am working in one of the DIRs I have in the global ~/.gitconfig, eg. /mnt/c/Users/myname/Documents/work/github it should be using the appropriate referenced config with the username and email in the config, but it is not.

If I run: git config --list I get this:

includeif.gitdir:/mnt/c/Users/myname/Documents/work/github.path=/mnt/c/Users/myname/.gitconfig-github
includeif.gitdir:/mnt/c/Users/myname/Documents/work/gitlab.path=/mnt/c/Users/myname/.gitconfig-gitlab
init.defaultbranch=main

Then if I run: git config user.name

I should get back the username depending on which directory I am in (gitlab or github). Instead I get a blank return. If I am in the right directory, it should be using either of the two config files I referenced in the global config.

Despite finding multiple guides that this is meant to be the way to do this. It does not work. git always asks me for my username and password when I run a git clone.

Many of us have multiple accounts on multiple platforms. So what is the proper way to do this?

1

There are 1 best solutions below

0
pythonInRelay On

I found the answer elsewhere. The syntax changed silently.

The gitdir: line must have the .git ending, so the global .gitconfig file should look as such:

[includeIf "gitdir:/mnt/c/Users/myname/Documents/work/github/**/.git"]
  path = /mnt/c/Users/myname/.gitconfig-github

[includeIf "gitdir:/mnt/c/Users/myname/Documents/work/gitlab/**/.git"]
  path = /mnt/c/Users/myname/.gitconfig-gitlab

[init]
        defaultBranch = main

Also by adding the glob /**/ I can cover any git repo in eg. .../gitlab/myrepo1

I now get the correct response when inside a git repo folder:

git config -l   

includeif.gitdir:/mnt/c/Users/myname/Documents/work/github/**/.git.path=/mnt/c/Users/myname/.gitconfig-github
includeif.gitdir:/mnt/c/Users/myname/Documents/work/gitlab/**/.git.path=/mnt/c/Users/myname/.gitconfig-gitlab
gpg.format=ssh
commit.gpgsign=true
user.name=mygithubname
[email protected]