Solutions for Pushing to GitHub with Different Accounts on the Same Computer

Nov 18, 2025 · Programming · 17 views · 7.8

Keywords: Git Configuration | GitHub Multi-Account | SSH Authentication | HTTPS Authentication | Permission Management

Abstract: This article provides comprehensive solutions for Git push permission issues when using different GitHub accounts on the same computer. It covers Git configuration management, SSH key handling, and HTTPS authentication mechanisms, offering multiple approaches including local Git configuration overrides, SSH key switching, and HTTPS credential reset. The content includes detailed code examples and configuration steps to help developers understand Git authentication workflows and resolve multi-account management challenges in practical development scenarios.

Problem Background and Error Analysis

In shared development environments, when multiple users utilize Git for version control on the same computer, permission authentication conflicts frequently occur. A typical error message appears as:

$ git push her_github_repository our_branch
ERROR: Permission to her_username/repository.git denied to my_username.
fatal: The remote end hung up unexpectedly

This error indicates that Git continues to use previously configured username for authentication instead of the account required for current operations. This situation is particularly common in team collaborations or when individuals use multiple GitHub accounts.

Git Authentication Mechanism Analysis

Git supports two main remote repository access protocols: SSH and HTTPS. Each protocol employs different authentication methods:

SSH Protocol Authentication

The SSH protocol uses public key cryptography for identity verification. When establishing SSH connections, Git searches for local SSH key pairs and associates public keys with GitHub accounts. Proper SSH key configuration management becomes crucial when multiple users share the same computer.

HTTPS Protocol Authentication

The HTTPS protocol utilizes username and password (or access tokens) for authentication. Since August 13, 2021, GitHub has removed support for password authentication, mandating the use of Personal Access Tokens for HTTPS operations. This change enhances security but increases configuration complexity.

Primary Solutions

Method 1: Local Git Configuration Override

Within specific Git repository directories, you can override global configurations by setting local username and email:

git config user.name her_username
git config user.email her_email

This method only affects the current repository's configuration without interfering with other repositories. By omitting the -g flag, configurations are saved in the local .git/config file rather than global settings.

Method 2: SSH Key Management

For SSH protocol usage, generate independent SSH key pairs for each user:

ssh-keygen -t ed25519 -C "her_email@example.com" -f ~/.ssh/her_github_key

Then add the public key to the corresponding GitHub account. Configure host aliases in the ~/.ssh/config file:

Host github-her
    HostName github.com
    User git
    IdentityFile ~/.ssh/her_github_key

When using, modify the remote repository URL to: git@github-her:her_username/repository.git

Method 3: HTTPS Authentication Reset

For HTTPS protocol, clear cached credentials and re-authenticate:

git config --local credential.helper ""
git push origin master

After executing these commands, Git will prompt for username and personal access token, ensuring authentication with the correct account.

Configuration Verification and Debugging

Checking Current Configuration

Use the following commands to examine current Git configurations:

git config --list
git config --global --list
git config --local --list

These commands display all configurations, global configurations, and local configurations respectively, helping identify configuration conflicts.

Verifying Remote Repository Configuration

Check remote repository URL configurations:

git remote -v

Ensure remote repository URLs point to correct GitHub accounts and repositories.

Best Practice Recommendations

Environment Isolation Strategy

In shared computer environments, creating separate Windows user accounts for each user is recommended. This approach provides independent SSH keys, Git configurations, and credential caches for each user, fundamentally avoiding configuration conflicts.

Credential Management Tools

Utilizing Git Credential Manager or similar credential management tools can simplify multi-account management. These tools securely store and manage access tokens for multiple GitHub accounts.

Regular Cache Cleaning

Regularly cleaning Git credential caches prevents old authentication information from interfering with current operations:

git credential-cache exit

Common Issue Troubleshooting

Persistent Permission Errors

If permission errors persist after configuration changes, check the following aspects:

Authentication Prompts Not Appearing

If Git doesn't prompt for credentials, credential helpers might cache old authentication information. Clear cache and reconfigure credential helpers:

git config --global --unset credential.helper

Conclusion

Managing multiple GitHub accounts on the same computer requires careful configuration management. By understanding Git authentication mechanisms and properly utilizing local configuration overrides, SSH key management, and HTTPS authentication reset methods, permission conflicts can be effectively resolved. It's recommended to select the most suitable solution based on specific usage scenarios and establish standardized configuration management procedures.

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.