I have two keys to different github account and there is the config ~/.gitconfig:
[user]
        name = example
        email = example@example.com
[pull]
        rebase = true
[rebase]
        autoStash = true
[filter "lfs"]
        clean = git-lfs clean -- %f
        smudge = git-lfs smudge -- %f
        process = git-lfs filter-process
        required = true
[includeIf "gitdir/i:/Users/example/Documents/github_2/"]
    [core]
        sshCommand = "ssh -i ~/.ssh/github2_key"

then, I go to a folder that did not at /Users/example/Documents/github_2/, and run git clone github1_private_project, git told me Please make sure you have the correct access rights
It works to git clone the project from account 'github2' in /Users/example/Documents/github_2/

git version:
% git -v
git version 2.39.2 (Apple Git-143)


why .gitconfig [includIf] override the default config
The syntax for includeIf should be (see the docs):
[includeIf "gitdir/i:/Users/example/Documents/github_2/"]
path = </path/to/includeFile>

The syntax
[includeIf "gitdir/i:/Users/example/Documents/github_2/"]
    [core]
        sshCommand = "ssh -i ~/.ssh/github2_key"

actually means
[includeIf "gitdir/i:/Users/example/Documents/github_2/"]
# No `path` hence no include

[core]
    sshCommand = "ssh -i ~/.ssh/github2_key"

where the key core.sshCommand is always (unconditionally) defined.
