- Generate a new ssh key.
ssh-keygen -t ed25519 -C "personal@account.com" -f ~/.ssh/git_personal_account
- Update your
~/.ssh/config
file to automatically load keys into the ssh-agent. If you set a passphrase for the ssh key, make sure to addUseKeychain yes
also.
# ~/.ssh/config
# Repeat this for all accounts:
Host *
AddKeysToAgent yes
IdentityFile ~/.ssh/git_personal_account
- Repeat the previous steps for all accounts.
- Update your
~/.gitconfig
to conditionally load the account settings based on directory.
# ~/.gitconfig
[core]
autocrlf = input
[init]
defaultBranch = main
[gpg]
format = ssh
[commit]
gpgsign = true
[push]
autoSetupRemote = true
[user]
useConfigOnly = true
# Repeat this for all accounts:
# Include for all repositories inside ~/code/personal/
[includeIf "gitdir:~/code/personal/"]
path = ~/code/personal/.gitconfig
- In the directory you created for this specific account add a
.gitconfig
file. Repeat for all accounts.
# ~/code/personal/.gitconfig
[user]
name = Personal
email = personal@account.com
signingkey = ~/.ssh/git_personal_account
[core]
sshCommand = "ssh -i ~/.ssh/git_personal_account"
With one account added your file structure should look something like this:
~
+-- .ssh
| +-- config
| +-- git_personal_account ; account's private key
| +-- git_personal_account.pub ; account's public key
+-- .gitconfig ; the global git config
+-- code ; a directory where you keep the code for all your accounts
| +-- personal ; one account
| | +-- .gitconfig ; git config specific to the account
...
- Copy the public ssh keys contents
pbcopy < ~/.ssh/git_personal_account.pub
and add it as asign in key
andauthentication key
to your git provider, ex. GitHub.