20250418-a_tale_of_two_gits.md (1645B)
1 --- 2 title: a_tale_of_two_gits 3 keywords: [git, linux, programming, ssh, tips] 4 ... 5 6 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. 7 8 9 This is my main Git config 10 ``` 11 [init] 12 defaultbranch = main 13 [user] 14 email = root@pyratebeard.net 15 name = pyratebeard 16 username = pyratebeard 17 signingkey = 0xC7877C715113A16D 18 useconfigonly = true 19 [includeif "gitdir:/home/pyratebeard/src/suit/"] 20 path = ~/.config/git/config.suit 21 ``` 22 23 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. 24 25 This is _config.suit_ 26 ``` 27 [user] 28 email = captain@mail.com 29 name = captain beard 30 username = capn 31 signingkey = 0x766A144AC7 32 useconfigonly = true 33 [url "git@gitlab.com-suit"] 34 insteadof = git@gitlab.com 35 ``` 36 37 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. 38 ``` 39 Host gitlab.com 40 HostName gitlab.com 41 User git 42 IdentityFile ~/.ssh/pyratebeard_gpg_ssh-gitlab.pub 43 44 Host gitlab.com-suit 45 HostName gitlab.com 46 User git 47 IdentityFile ~/.ssh/suit_gpg_ssh.pub 48 ``` 49 50 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.