gitgit-config

Git - includeIf hasconfig:remote.*.url not working


I want to use two separate GitHub accounts on the same computer so I setup ssh keys for both of them. Both are working fine. I don't want to config my email and name each time I create a new repo so I looked around and found git's "includeIf" section.

I am using git version 2.37.3

These are my config files right now.

~/.gitconfig

[user]
    email = "home@example.com"
    name = "Home"

[includeIf "hasconfig:remote.*.url:git@github.com-Work:*/**"]
    email = "work@example.com"
    name = "Work"

~/.ssh/config

Host github.com-Home
  HostName github.com
  User git
  IdentityFile ~/.ssh/Home

Host github.com-Work
  HostName github.com
  User git
  IdentityFile ~/.ssh/Work

When I clone a repo like git clone git@github.com-Home:Home/repo.git and run git config user.name inside the repo, I get the expected output of Home

However, when I clone a repo for my work account like git clone git@github.com-Work:Work/repo.git and run git config user.name inside the repo, I get Home instead of Work. Running git config remote.origin.url returns git@github.com-Work:Work/repo.git

Any idea of why this isn't working?


Solution

  • An includeIf... statement only allows, as its name suggests, to include another file (as opposed to: a config section within the same file).

    You would have to put your email, name parameters in a separate file, and point to that file using path = ... in your main config file.


    quoting the relevant example from git help config:

    ; include only if a remote with the given URL exists (note
    ; that such a URL may be provided later in a file or in a
    ; file read after this file is read, as seen in this example)
    [includeIf "hasconfig:remote.*.url:https://example.com/**"]
        path = foo.inc
    

    The pattern git@github.com-Work:*/** should be enough.