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-core
in 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
repo
scope. 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-core
in 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
repo
scope. 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-core
in 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
repo
scope. 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_COMMAND
accordingly. 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