Git Configuration Based on Remote Repositories
Oct 3 (21d ago)
6 views
Git applies settings from your ~/.gitconfig to every repository. This is useful for defaults but becomes a problem when repositories or remotes need different identities or settings, for example:
These situations often require editing or overriding your global config.
includeIf Conditional ConfigurationGit's includeIf directive allows you to conditionally include configuration files based on certain criteria, such as the presence of a specific remote URL.
hasconfig:remote.*.url checks if any remote URL matches the specified pattern. This enables you to load different configuration files based on the remote repository's host.
How it works
~/.ssh/config to select the right SSH key per host.includeIf to load host-specific .gitconfig only when a repository uses that remote.This is for Windows, but the same principles apply to Linux and macOS. Just adjust file paths accordingly.
I am using different SSH keys for authentication and signing commits for GitHub and GitLab. You can use the same key for both, but I prefer separate keys.
~/.ssh/config# github
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_auth
IdentitiesOnly yes
# gitlab
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_gitlab_auth
IdentitiesOnly yes
~/.ssh/configis optional but useful if you use different SSH keys for different hosts. It ensures the correct key is used for authentication.
~/.gitconfig-gitlab[user]
signingkey = C:\\Users\\username\\.ssh\\id_ed25519_gitlab_sign.pub~/.gitconfig to include the GitLab config when a repository uses a GitLab remote.[user]
name = thisisrick25
email = swapnaneel06@gmail.com
signingkey = C:\\Users\\username\\.ssh\\id_ed25519_github_sign.pub
[filter "lfs"]
clean = git-lfs clean -- %f
smudge = git-lfs smudge -- %f
process = git-lfs filter-process
required = true
[gpg]
format = ssh
[commit]
gpgsign = true
[includeIf "hasconfig:remote.*.url:*gitlab.com*/**"]
path = ~/.gitconfig-gitlab
[includeIf "hasconfig:remote.*.url:https://gitlab.com/**"]
path = ~/.gitconfig-gitlab
gitlab.com*/**is used for SSH remotes for GitLab.https://gitlab.com/**is used for HTTPS remotes for GitLab.