Quick Links

If you've tried to use Git with multiple different providers, or with multiple different accounts, you may run into issues with authenticated them both at the same time. Here's how to manage two accounts without running into conflicts.

How Git Handles Accounts

The first thing to understand is that Git is entirely local---your local Git repository isn't controlled by the server you use to store code on. It only connects to the remote repository when pushing or pulling updates. When it does this, it uses your Git account details.

The only details it uses when doing so are your email and username. The password for the account is asked every time, or stored in the keychain, or irrelevant if you're using SSH keys (which you should). If your username is different than the account you have credentials for, you won't be able to push or pull until you fix it. In some cases, if you use the same email for different accounts, the push may go through but show up with a different username in the commits, which may not be ideal.

These details are defined in

        git
    

's config. You've probably had to run the following commands to change your user account name and email:

git config --global user.name username
    

git config --global user.email email

This will set the global config to the new username and email. However, another issue is the result. Because it sets it globally, if you want to checkout a project on a different account, you'll break the settings for your other repositories.

The solution is to set a custom config for each repository that uses a different account. Set your global username and email to your most often used account, as it will be the default for all new repositories and repositories without any configuration.

However, for each repository that uses a different account, you'll want to leave out the

--global flag:

git config user.name username
    

git config user.email email

This overrides the default config, so even if you change your default account, the local account will still be used for this repository.

Managing Credentials for Multiple Accounts

You'll want to use SSH keys anyway for Git, so credential management should be a non-issue. It's perfectly acceptable to use the same SSH key everywhere---your SSH key is tied to your machine, and in turn, your identity. It's different than a password; there's no way to crack it without hacking into your personal computer, which is much less likely to happen than a data breach.

However, if you need to use different SSH keys for some reason, it's possible to do so. First, you'll want to ensure the secondary SSH key is loaded into your keychain, usually by editing ~/.bashrc to call ssh-add when you load the terminal:

ssh-add ~/.ssh/secondary

Otherwise, you'll have to use the -i flag and specify the key path each time.

To make Git use different keys for different accounts, you'll want to edit ~/.ssh/config and add a Host block for each account:

# Personal account, - the default config
    

Host github.com

HostName github.com

User git

IdentityFile ~/.ssh/id_rsa

# Work account-1

Host github.com:secondary

HostName github.com

User git

IdentityFile ~/.ssh/secondary

This will use different keyfiles based on the hostname following git@ in the Git URL. Github appends your username to the Git URL, so it's easy to differentiate between users.