Blog

Managing Git Personas

January 24, 2021

#howto

Last year I started using a split Git configuration that specifies different “personas” for different sets of repositories. What I’m calling a persona here comprises the author name and email information that gets put into Git commits, and sometimes it also involves different auth credentials. This system works well, but I always forget how to set it up. So this post is basically documentation for myself.

Our example config here will be for a person, Alice, that wants to add a special config for accessing some personal, self-hosted repositories from her work laptop. We’ll configure Git to automatically use work commit info for all repositories within a ~/gitwork/ directory and personal commit info for all repositories within a ~/githome/ directory. Alice also uses a separate SSH key for her home repositories, so we’ll set that up to automatically get used for ~/githome/.

Main Config (~/.gitconfig)

[user]
name = Anonymous Author
email = anon@mous.foo

[includeIf "gitdir:~/gitwork/"]
path = .gitconfig-work

[includeIf "gitdir:~/githome/"]
path = .gitconfig-home

The main mechanism here is that we conditionally include child configs based on the repository path. Optionally, we can include some fallback settings, but it is important that they are defined before the include sections so that the child configs can override the settings if need be.

Work Config (~/.gitconfig-work)

[user]
name = Alice Hackworth
email = alice.hackworth@bigco.foo

This config contains Alice’s professional commit info. Nothing fancy here.

Home Config (~/.gitconfig-home)

[user]
name = Alice
email = alice@home.foo

[core]
sshCommand = ssh -i ~/.ssh/personal_ed25519

Here we have Alice’s casual commit info along with a special SSH key for accessing her home server. Nifty!