Skip to content

Instantly share code, notes, and snippets.

@raahede
Last active October 13, 2023 14:32
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save raahede/3342f3821babee6104ee7ccb8eff7291 to your computer and use it in GitHub Desktop.
Save raahede/3342f3821babee6104ee7ccb8eff7291 to your computer and use it in GitHub Desktop.
Handle multiple GitHub accounts on Mac

Multiple identities

Inspired by article How to Handle Multiple Git Accounts

Work with multiple SSH keys for authorization and commit signing.

Create SSH key

  1. Generate a new ssh key
ssh-keygen -t ed25519 -C "firstname@work-email.com"
  1. Use custom name, e.g. github-work-user
  2. Add passphrase to your password manager (e.g. 1Password)
  3. Add ssh key to keychain
ssh-add --apple-use-keychain ~/.ssh/github-work-user
  1. 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

Add ssh key to github

Adding a new SSH key to your GitHub account

  1. Copy content of SSH key
pbcopy < ~/.ssh/github-work-user.pub
  1. Add the public key as Authentication Key in /settings/keys
  2. Add the public key as Signing Key in /settings/keys

Create gitconfig

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 = firstname@work-email.com
    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 = firstname@lastname.com
    signingkey = ~/.ssh/github-personal-user.pub
[commit]
    gpgsign = true
[core]
    sshCommand = "ssh -i ~/.ssh/github-personal-user"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment