Contributing to multiple accounts using HTTPS and personal access tokens
Alternatively, if you want to use the HTTPS protocol for both accounts, you can use different personal access tokens for each account by configuring Git to store different credentials for each repository.
-
Open Terminal.
-
To confirm your use of a credential manager, enter the following command and note the output.
Shell git config --get credential.helper
git config --get credential.helper -
If the output confirms that you're using a credential manager, clear the stored credentials for the credential manager.
-
If the output doesn't include the name of a credential manager, there is no credential manager configured, and you can proceed to the next step.
-
If the output is
osxkeychain, you're using the macOS keychain. To clear the credentials, you can use the credential helper on the command line:$ git credential-osxkeychain erase host=github.com protocol=https > [Press Return] > -
If the output is
manager(ormanager-corein previous versions), you're using Git Credential Manager. To clear the credentials, run the following command.Shell echo "protocol=https\nhost=github.com" | git credential-manager erase
echo "protocol=https\nhost=github.com" | git credential-manager erase
-
-
To configure Git to cache credentials for the full remote URL of each repository you access on GitHub, enter the following command.
Shell git config --global credential.https://github.com.useHttpPath true
git config --global credential.https://github.com.useHttpPath true -
For each of your accounts, create a dedicated personal access token (classic) with
reposcope. Or, for each of your accounts and for each organization that you are a member of, create a fine-grained personal access token that can access the desired repositories and that has read and write permissions on repository contents. For more information, see Managing your personal access tokens. -
The first time that you use Git to clone a repository or access data in a repository that you've already cloned, Git will request credentials. Provide the personal access token for the account with access to the repository.
Git will cache the personal access token based on the full remote URL of the repository, and you'll be able to access and write repository data on GitHub.com using the correct account.
-
Open Git Bash.
-
To confirm your use of a credential manager, enter the following command and note the output.
Shell git config --get credential.helper
git config --get credential.helper -
If the output confirms that you're using a credential manager, clear the stored credentials for the credential manager.
- If the output doesn't include the name of a credential manager, there is no credential manager configured, and you can proceed to the next step.
- If the output is
manager(ormanager-corein previous versions), you're using Git Credential Manager. To clear the credentials, run the following command.
Shell echo "protocol=https`nhost=github.com" | git credential-manager erase
echo "protocol=https`nhost=github.com" | git credential-manager erase-
If the output is
wincred, you're using the Windows Credential Manager. To clear the credentials, enter the following command.Shell cmdkey /delete:LegacyGeneric:target=git:https://github.com
cmdkey /delete:LegacyGeneric:target=git:https://github.com
-
To configure Git to cache credentials for the full remote URL of each repository you access on GitHub, enter the following command.
Shell git config --global credential.https://github.com.useHttpPath true
git config --global credential.https://github.com.useHttpPath true -
For each of your accounts, create a dedicated personal access token (classic) with
reposcope. Or, for each of your accounts and for each organization that you are a member of, create a fine-grained personal access token that can access the desired repositories and that has read and write permissions on repository contents. For more information, see Managing your personal access tokens. -
The first time that you use Git to clone a repository or access data in a repository that you've already cloned, Git will request credentials. Provide the personal access token for the account with access to the repository.
Git will cache the personal access token based on the full remote URL of the repository, and you'll be able to access and write repository data on GitHub.com using the correct account.
-
Open Terminal.
-
To confirm your use of a credential manager, enter the following command and note the output.
Shell git config --get credential.helper
git config --get credential.helper -
If the output confirms that you're using a credential manager, clear the stored credentials for the credential manager.
- If the output doesn't include the name of a credential manager, there is no credential manager configured, and you can proceed to the next step.
- If the output is
manager(ormanager-corein previous versions), you're using Git Credential Manager. To clear the credentials, run the following command.
Shell echo "protocol=https\nhost=github.com" | git credential-manager erase
echo "protocol=https\nhost=github.com" | git credential-manager erase -
To configure Git to cache credentials for the full remote URL of each repository you access on GitHub, enter the following command.
Shell git config --global credential.https://github.com.useHttpPath true
git config --global credential.https://github.com.useHttpPath true -
For each of your accounts, create a dedicated personal access token (classic) with
reposcope. Or, for each of your accounts and for each organization that you are a member of, create a fine-grained personal access token that can access the desired repositories and that has read and write permissions on repository contents. For more information, see Managing your personal access tokens. -
The first time that you use Git to clone a repository or access data in a repository that you've already cloned, Git will request credentials. Provide the personal access token for the account with access to the repository.
Git will cache the personal access token based on the full remote URL of the repository, and you'll be able to access and write repository data on GitHub.com using the correct account.
Contributing to multiple accounts using SSH and GIT_SSH_COMMAND
If you want to use the SSH protocol for both accounts, you can use different SSH keys for each account. For more information about using SSH, see Connecting to GitHub with SSH.
To use a different SSH key for different repositories that you clone to your workstation, you must write a shell wrapper function for Git operations. The function should perform the following steps.
- Determine the repository's full name with owner, using a command such as
git config --get remote.origin.url. - Choose the correct SSH key for authentication.
- Modify
GIT_SSH_COMMANDaccordingly. For more information aboutGIT_SSH_COMMAND, see Environment Variables in the Git documentation.
For example, the following command sets the GIT_SSH_COMMAND environment variable to specify an SSH command that uses the private key file at PATH/TO/KEY/FILE for authentication to clone the repository named OWNER/REPOSITORY on GitHub.com.
GIT_SSH_COMMAND='ssh -i PATH/TO/KEY/FILE -o IdentitiesOnly=yes' git clone git@github.com:OWNER/REPOSITORY
GIT_SSH_COMMAND='ssh -i PATH/TO/KEY/FILE -o IdentitiesOnly=yes' git clone git@github.com:OWNER/REPOSITORY
Contributing to multiple accounts using SSH and multiple keys
If you are a member of an enterprise with managed users, but also want to collaborate outside your enterprise using a personal account, you can use different SSH keys for each account. For more information about using SSH, see Connecting to GitHub with SSH.
Warning
You cannot use the same SSH key to contribute to both repositories inside your organization with managed users and outside the enterprise.
-
Generate a different SSH key for the repositories in your organization with managed users. See Generating a new SSH key and adding it to the ssh-agent. When you save the key, give it a different filename from your existing key (for instance, add -emu to the suggested name of the file).
-
Add the new ssh key to your managed user account. See Adding a new SSH key to your GitHub account
-
Configure your SSH Config File
~/.ssh/configto use the different keys. For example, if your personal SSH key is~/.ssh/id_ed25519and your enterprise with managed users SSH key is~/.ssh/id_ed25519-emuText Host github.com IdentityFile ~/.ssh/id_ed25519 IdentitiesOnly yes Host github-emu.com Hostname github.com IdentityFile ~/.ssh/id_ed25519-emu IdentitiesOnly yesHost github.com IdentityFile ~/.ssh/id_ed25519 IdentitiesOnly yes Host github-emu.com Hostname github.com IdentityFile ~/.ssh/id_ed25519-emu IdentitiesOnly yesNote
The
IdentitiesOnlyline ensures that if the ssh-agent has loaded multiple keys, ssh uses the correct key when connecting. -
Test your SSH configuration by running the following command to connect using the SSH key associated with your personal account - see Testing your SSH connection for further details
Shell ssh -T git@github.com
ssh -T git@github.comTest to see if you can connect to (GitHub) using your enterprise with managed users SSH key
Shell ssh -T git@github-emu.com
ssh -T git@github-emu.com -
Tell
gitto use the correct key when downloading or uploading a repository in an organization with managed users. To list the organizations in your enterprise with managed users,- In the upper-right corner of GitHub, click your profile picture, then click Organizations.
- For each organization listed tell
gitto use thegithub-emu.comhost.
For example, if one of your organizations is called
octocat-emuthen to tellgitto use the hostgithub-emu.comfor repositories in theoctocat-emuorganization, run the following commandShell git config --global url."git@github-emu.com:octocat-emu/".insteadOf "git@github.com:octocat-emu/"
git config --global url."git@github-emu.com:octocat-emu/".insteadOf "git@github.com:octocat-emu/"
Now, when you clone a repository using SSH, in the octocat-emu organization, git will use the SSH key associated with your enterprise with managed users instead of your personal key.
Next steps
For reference information, see Personal account reference.