Skip to content

Managing multiple Git identities

I have multiple Git identities (personal/school/work), and having to manually specify which to use each time quickly got tedious. Luckily, Git has conditional includes.

Initially, I used gitdir/i to include the relevant configuration file based on the repository’s case-insensitive path:

# ~/.config/git/config
[includeIf "gitdir/i:personal/"]
	path = "config.d/personal"
[includeIf "gitdir/i:school/"]
	path = "config.d/school"

# ~/.config/git/config.d/personal
[user]
	email = name@provider.tld

# ~/.config/git/config.d/school
[user]
	email = name@school.tld

However, since Git 2.36.0 I use a combination of hasconfig:remote.*.url and SSH’s Host keyword to include based on the remote’s URL:

# ~/.config/git/config
[includeIf "hasconfig:remote.*.url:github-personal:**/**"]
	path = config.d/personal
[includeIf "hasconfig:remote.*.url:github-school:**/**"]
	path = config.d/school
# ~/.ssh/config
Host github-*
    User git
    Hostname github.com
    IdentityFile ~/.ssh/keys/github

With this, using an identity is just a matter of git clone github-personal:….