Skip to main content

Conditional Git Config

·138 words·1 min
Git Ssh Gpg

To have a releavant git config for different repos, use includeIf (for condition on remote url git version must be >= 2.46.0 )

Example with Remote URL
#

You’ve got multiple ssh keys for a host user1.gh and user2.gh:

Host user1.gh
    HostName github.com
    User user1
    IdentityFile ~/.ssh/github/user1/id_ed25519

and you use ssh link as a remote url:
git clone git@user1.gh:<user>/<repo>

to automate git configuration, create a config specific to this host, create a file like:
~/gitconfigs/user1.gh.config
and put configuration you need there, like:

[user]
    email = user1@email.net
    signingkey = <key>
[commit]
    gpgsign = true

and include this config to a global config (git config --global -e):

[includeIf "hasconfig:remote.*.url:git@user1.gh:*/**"]
  path = /home/<user>/gitconfigs/user1.gh.config

verify if it has worked (in directory with relevant repo):
git config --list --show-origin
conditional config should be shown.

Sources
#