Skip to content

Instantly share code, notes, and snippets.

@savaryna
Last active February 17, 2023 23:21
Show Gist options
  • Save savaryna/671f2835c9d488687269fbbbd0f6999c to your computer and use it in GitHub Desktop.
Save savaryna/671f2835c9d488687269fbbbd0f6999c to your computer and use it in GitHub Desktop.
Setting up GIT on MacOS with multiple accounts.

Setting up GIT on MacOS with multiple accounts.

Setting up SSH

  1. Generate a new ssh key.
ssh-keygen -t ed25519 -C "personal@account.com" -f ~/.ssh/git_personal_account
  1. 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 add UseKeychain yes also.
# ~/.ssh/config

# Repeat this for all accounts:
Host *
  AddKeysToAgent yes
  IdentityFile ~/.ssh/git_personal_account
  1. Repeat the previous steps for all accounts.

Setting up GIT

  1. 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
  1. 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
...
  1. Copy the public ssh keys contents pbcopy < ~/.ssh/git_personal_account.pub and add it as a sign in key and authentication key to your git provider, ex. GitHub.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment