Complete Guide to Managing Multiple GitHub Accounts on the Same Computer

Nov 15, 2025 · Programming · 11 views · 7.8

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_work

Repeat 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_personal

This 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.pub

Then 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.git

Or to update the remote URL for an existing repository:

git remote set-url origin github-personal:username/repository.git

HTTPS 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 true

This 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.git

On 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.com

Conditional Configuration

Override user settings for repositories in specific directories. Add to the global configuration:

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

Then set the specific information for the work account in ~/work/.gitconfig:

[user]
    name = Work Name
    email = work@company.com

File 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.pub

These 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.git

Or from HTTPS to SSH:

git remote set-url origin github-personal:username/repository.git

Advanced 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 4096

Extract the public key from the PEM file:

ssh-keygen -y -f ~/.ssh/github-work.pem

Use the PEM file in the SSH configuration:

Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-work.pem

Troubleshooting

If authentication issues arise, check the following common causes:

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.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.