How to use multiple git accounts on the same machine

This method will automatically choose a git account to use based on the work directory. You can add as many git accounts as you want.

1. Create SSH keys

Follow steps from GitHub here.

  • Please name your keys differently for each account. e.g. ~/.ssh/id_ed25519_personal, ~/.ssh/id_ed25519_work
  • Skip editing ~/.ssh/config part, because we gonna specify which key to use using git config
  • Need to add identity to keychain
# Generate a key, don't forget to rename the file
ssh-keygen -t ed25519 -C "your_email@example.com"

# Add key to keychain
ssh-add --apple-use-keychain ~/.ssh/id_ed25519

2. Separate your git repositories into directories

E.g. ~/workspace/work/, ~/workspace/personal/

3. Config global gitconfig

Edit your ~/.gitconfig as example

includeIf tells git agent to include gitconfig from specified path if we are in the specified gitdir

* trailing backslash is important, don’t drop it
* /i in gitdir/i is for insensitive
* path is relative to root directory

[includeIf "gitdir/i:~/workspace/work/"]
    path = .gitconfig-work

[includeIf "gitdir/i:~/workspace/personal/"]
    path = .gitconfig-personal

4. Config gitconfig for each account

In ~/.gitconfig-work and ~.gitconfig-personal, configure based on the account. Let’s configure email, name, and ssh identity here

[user]
    email = your-email@work.com
    name = Firstname Lastname
[core]
    sshCommand = "ssh -i ~/.ssh/id_ed25519_work"

This means when you run any git command, git will use ssh -i/.ssh... instead of ssh command

You can place the config file any where
Create new config files anywhere you want. Make sure you provide the correct path in the global git config in the previous step (~/.gitconfig)

In the example, I create them in the root directory, so every config file will be in the same place

5. Check

Go to any git repository in your work workspace and your personal workspace. Run this command. You should see your configured username correctly

git config --get-all user.name

or check which ssh command is being used by cloning any git repository using the ssh url (git@github:username/repo.git)

You should see the correct ssh command being used.

GIT_TRACE=1 git clone git@github.com:username/repo.git

output

# you should see output like this
...
trace: run_command: unset GIT_DIR; GIT_PROTOCOL=version=2 'ssh -i ~/.ssh/id_ed25519_work' -o SendEnv=GIT_PROTOCOL git@github.com 'git-upload-pack '\''username/repo.git'\'''
...

Older revision (A harder way)

This method will automatically choose a git account to use based on work directory. You can add as many git accounts as you want.

Related files

All files we gonna make change to

  • ~/.ssh/config
  • ~/.gitconfig
  • ~/workspace/work/.gitconfig
  • ~/workspace/personal/.gitconfig

Create ssh keys

Follow steps from github here.
Please name your keys differently for each account. e.g. ~/.ssh/id_ed25519-personal, ~/.ssh/id_ed25519-work

Config ssh keys

In ~/.ssh/config config it like this. Use different identity file for different host. You can name your host anything; we will use it in the next step.

# Personal GitHub
Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519-personal
    IdentitiesOnly yes

# Work GitHub
Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519-work
    IdentitiesOnly yes

Host *
    AddKeysToAgent yes
    UseKeychain yes

Config global gitconfig

We gonna make git agent know when to use which account based on workspace.
If we are in ~/workspace/work, it will use ~/workspace/work/.gitconfig
If we are in ~/workspace/personal, it will use ~/workspace/personal/.gitconfig

Edit your ~/.gitconfig

[includeIf "gitdir/i:~/workspace/work/"]
    path = ~/workspace/work/.gitconfig

[includeIf "gitdir/i:~/workspace/personal/"]
    path = ~/workspace/personal/.gitconfig

includeIf tells git agent to include .gitconfig in the specified path if we are in the specified gitdir.

Config gitconfig for each account

In ~/workspace/work/.gitconfig and ~/workspace/personal/.gitconfig, config based on a git account sits here. We will config email/name and rewrite github host here.

[user]
    email = your-email@gmail.com
    name = git-username

[url "git@github-personal"]
    insteadOf = git@github.com

Check

Go to any git repository in your work workspace and your personal workspace. Run this command. You should see your configured username correctly.

git config --get-all user.name

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *