Inspired by article How to Handle Multiple Git Accounts
Work with multiple SSH keys for authorization and commit signing.
ssh-keygen -t ed25519 -C "[email protected]"
- Use custom name, e.g.
github-work-user
- Add passphrase to your password manager (e.g. 1Password)
- Add ssh key to keychain
ssh-add --apple-use-keychain ~/.ssh/github-work-user
- Ensure that ssh keys from Github use keychain (otherwise you need to type passphrase for each git commit/push/pull etc)
~/.ssh/config
Host github.com
AddKeysToAgent yes
UseKeychain yes
Adding a new SSH key to your GitHub account
- Copy content of SSH key
pbcopy < ~/.ssh/github-work-user.pub
- Add the public key as
Authentication Key
in/settings/keys
- Add the public key as
Signing Key
in/settings/keys
Global git config points to local configs depending on folder to differentiate git users.
This assumes a folder structure for projects that looks like this. All projects in the work-projects/
folder will use the work github ssh key for authentication and signing.
~/Sites/
- work-projects/
- personal-projects/
~/.gitconfig
[gpg]
format = ssh
# The trailing slashes below are required to match
# any subdirectory and not just the exact path
[includeIf "gitdir:~/Sites/work-projects/"]
path = ~/.gitconfig.work-user
[includeIf "gitdir:~/Sites/personal-projects/"]
path = ~/.gitconfig.personal-user
Local git config used for folder ~/Sites/work-projects/
(change name and email)
~/.gitconfig.work-user
[user]
name = Firstname Lastname
email = [email protected]
signingkey = ~/.ssh/work-user.pub
[commit]
gpgsign = true
[core]
sshCommand = "ssh -i ~/.ssh/github-work-user"
Local git config used for folder ~/Sites/personal-projects/
(change name and email)
~/.gitconfig.personal-user
[user]
name = Firstname Lastname
email = [email protected]
signingkey = ~/.ssh/github-personal-user.pub
[commit]
gpgsign = true
[core]
sshCommand = "ssh -i ~/.ssh/github-personal-user"