Keywords: GitHub | SSH keys | multiple account management | Git configuration | authentication
Abstract: This article provides a comprehensive guide to configuring and using multiple GitHub accounts on a single computer, covering two primary methods: SSH key configuration and HTTPS personal access tokens. Through step-by-step instructions and code examples, it explains how to generate and manage SSH keys, configure SSH config files, set Git user identities, and use HTTPS protocol for authentication. The article also discusses file permission management, updating existing repositories, and ensuring commit attribution to the correct GitHub accounts.
Introduction
In modern software development, developers often need to manage both personal and work-related GitHub accounts simultaneously. However, default Git configurations typically support only single authentication, leading to permission issues when switching accounts. Based on highly-rated Stack Overflow answers and official GitHub documentation, this article presents two reliable solutions: SSH key management and HTTPS personal access tokens.
SSH Key Configuration Method
SSH (Secure Shell) is an encrypted network protocol widely used for authentication in Git operations. By generating separate SSH key pairs for each GitHub account and configuring them appropriately locally, seamless account switching can be achieved.
Generating SSH Keys
First, generate a dedicated SSH key for each GitHub account. Use the following command to generate an Ed25519 algorithm key (recommended for better security):
ssh-keygen -t ed25519 -C "your_email@example.com"When prompted, assign a descriptive name to the key, such as id_ed25519_work and id_ed25519_personal. This creates two files: a private key (e.g., id_ed25519_work) and a public key (e.g., id_ed25519_work.pub).
Adding SSH Keys to Agent
To enable the SSH agent to use the newly generated keys, run the following command:
ssh-add ~/.ssh/id_ed25519_workRepeat this step for each account's private key file.
Configuring SSH Config File
Create or edit the config file in the ~/.ssh directory to define host aliases for each GitHub account:
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personalThis configuration allows accessing GitHub through different host aliases (e.g., github-work and github-personal), each corresponding to a different authentication key.
Adding Public Keys to GitHub
Copy the content of each public key file to the SSH key settings of the corresponding GitHub account. Use the following command to view the public key:
cat ~/.ssh/id_ed25519_work.pubThen add the new key in the GitHub website under Settings -> SSH and GPG keys.
Configuring Git Remote Repositories
When cloning or setting up remote repositories, use the host aliases defined in the SSH configuration. For example, to clone a repository for the work account:
git clone github-work:username/repository.gitOr to update the remote URL for an existing repository:
git remote set-url origin github-personal:username/repository.gitHTTPS Personal Access Token Method
As an alternative to SSH, the HTTPS protocol combined with personal access tokens (PAT) can be used for authentication. This method is suitable for those who prefer HTTPS or cannot use SSH in their environment.
Generating Personal Access Tokens
In each GitHub account, generate a personal access token with repo scope permissions. Visit GitHub's Settings -> Developer settings -> Personal access tokens page to create the token.
Configuring Git Credential Caching
To ensure Git caches the correct credentials for each repository, set the following global configuration:
git config --global credential.https://github.com.useHttpPath trueThis setting makes Git cache credentials based on the full remote URL, thereby distinguishing repositories from different accounts.
Using HTTPS Remote URLs
Set the remote URL of the repository to the HTTPS format, including the username in the URL:
git remote set-url origin https://username@github.com/username/repository.gitOn the first operation, Git will prompt for credentials. Use the personal access token of the corresponding username as the password.
Git User Identity Configuration
To ensure commit messages correctly display author information, configure the user name and email address for each repository. Git version 2.13 and above supports conditional configuration based on directories.
Global Configuration
Set default user information in the global Git configuration file:
[user]
name = Default Name
email = default@example.comConditional Configuration
Override user settings for repositories in specific directories. Add to the global configuration:
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfigThen set the specific information for the work account in ~/work/.gitconfig:
[user]
name = Work Name
email = work@company.comFile Permission Management
Correct file permissions are crucial for the security of SSH keys. Ensure the ~/.ssh directory and key files have strict permissions:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/config
chmod 600 ~/.ssh/id_ed25519_work
chmod 600 ~/.ssh/id_ed25519_work.pub
chmod 600 ~/.ssh/id_ed25519_personal
chmod 600 ~/.ssh/id_ed25519_personal.pubThese permissions ensure that only the file owner can read or modify SSH-related files.
Updating Existing Repositories
For existing repositories that need to switch authentication methods, simply update the remote URL. For example, to switch from default SSH to a specific account:
git remote set-url origin github-work:username/repository.gitOr from HTTPS to SSH:
git remote set-url origin github-personal:username/repository.gitAdvanced SSH Management Techniques
For users managing multiple SSH keys, consider using PEM format key files to reduce file count. Generate a PEM format key using OpenSSL:
openssl genrsa -out ~/.ssh/github-work.pem 4096Extract the public key from the PEM file:
ssh-keygen -y -f ~/.ssh/github-work.pemUse the PEM file in the SSH configuration:
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/github-work.pemTroubleshooting
If authentication issues arise, check the following common causes:
- SSH keys not correctly added to the GitHub account
- Mismatch between host aliases in SSH config and remote URLs
- Incorrect file permission settings
- Old credential information cached in Git credentials
Use ssh -T github-work to test SSH connection and verify configuration.
Conclusion
Through SSH key configuration or HTTPS personal access tokens, multiple GitHub accounts can be efficiently managed on the same computer. The SSH method offers better security and automation, while the HTTPS method may be more accessible in certain network environments. Combined with Git's user identity configuration, all commits can be correctly attributed to the corresponding accounts. It is recommended to choose the appropriate method based on specific needs and security considerations, and regularly review and update access credentials.