Skip to content

Instantly share code, notes, and snippets.

@radzio
Forked from raahede/README.md
Created October 11, 2023 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radzio/506f62ea80fac4c9355423e0c03daaf0 to your computer and use it in GitHub Desktop.
Save radzio/506f62ea80fac4c9355423e0c03daaf0 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

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