Interacting with multiple Git hosts using different identities can be useful when there are many repositories in public and proprietary locations.
The includeIf
option can be used as a conditional include for a path so that your repos in a
GitHub path use your GitHub identity while other locations can use your work identity. In your ~/.gitconfig
add the following section.
[includeIf "gitdir:~/Documents/GitHub/**"]
path = ~/Documents/GitHub/.gitconfig
Then add ~/Documents/GitHub/.gitconfig
for that unique configuration.
[user]
email = linus@gmail.com
name = Linus
Next your SSH key for GitHub should be unique from your work identity so create a key for that purpose
using the same email address which is your primary with GitHub. For the filename select github_rsa
instead of the default of id_rsa
which is likely already in place.
ssh-keygen -t rsa -b 4096 -C
Next, update your SSH config at ~/.ssh/config
to use this unique SSH key with the host configuration for GitHub.
Host github.com
UseKeyChain yes
AddKeysToAgent yes
IdentityFile ~/.ssh/github_rsa
Finally, add the new key to your GitHub keys so that your account is authorized.