When working on personal and “work” code repositories on my PC I prefer to use different Git [user]
settings depending on the repo, using my meatspace handle for work. I have found the easiest way to achieve this is setting a rule in my Git config to use an alternative config based on the directory.
This is my main Git config
[init]
defaultbranch = main
[user]
email = root@pyratebeard.net
name = pyratebeard
username = pyratebeard
signingkey = 0xC7877C715113A16D
useconfigonly = true
[includeif "gitdir:/home/pyratebeard/src/suit/"]
path = ~/.config/git/config.suit
The important section is the includeif
at the end. This tells Git that if the repo is located under the $HOME/src/suit/ directory then pull in the settings from another config file.
This is config.suit
[user]
email = captain@mail.com
name = captain beard
username = capn
signingkey = 0x766A144AC7
useconfigonly = true
[url "git@gitlab.com-suit"]
insteadof = git@gitlab.com
The file has my alternative [user]
settings as well as an override for the Gitlab SSH settings. I then have two Gitlab hosts configured in my SSH config so when I’m pushing work code it uses my work SSH key.
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/pyratebeard_gpg_ssh-gitlab.pub
Host gitlab.com-suit
HostName gitlab.com
User git
IdentityFile ~/.ssh/suit_gpg_ssh.pub
Setting this up means I don’t have to remember to alter the Git configs for different users, as long as I keep all “work” related repos under the specified directory.