git clone with different username/account
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When you have multiple GitHub (or GitLab/Bitbucket) accounts, such as a work account and a personal account, the default git clone command authenticates with whichever credential is cached on your system. This means cloning a repo with the wrong account silently succeeds for public repos but fails for private ones, and pushes always go out under the wrong identity. Learning how to explicitly control which account is used for each clone saves you from permission errors and mis-attributed commits.
Embedding the Username in the HTTPS URL
The quickest way to clone with a specific account is to include the username directly in the URL:
Git will prompt you for the password or personal access token (PAT) for work-user. This overrides whatever default credential is stored in your system's credential manager.
For GitHub specifically, password authentication is no longer supported. You need to use a PAT as the password:
You can also embed the token directly (useful for CI, but avoid this in shell history on shared machines):
SSH Config with Multiple Keys
SSH is the preferred method for managing multiple accounts because you can assign a different key to each account without entering credentials repeatedly. The trick is to create host aliases in your ~/.ssh/config file.
First, generate separate keys for each account:
Then configure host aliases in ~/.ssh/config:
Now clone using the alias instead of github.com:
The IdentitiesOnly yes directive is important. Without it, the SSH agent may offer other keys first, and GitHub accepts whichever key matches first regardless of which account you intended.
Using GIT_SSH_COMMAND
For a one-off clone with a specific SSH key without modifying your SSH config, use the GIT_SSH_COMMAND environment variable:
To make this persistent for a specific repository after cloning:
Git Credential Helpers
Git credential helpers cache or store your HTTPS credentials. The problem with multiple accounts is that the default helpers store one credential per host. When you need two GitHub accounts over HTTPS, you need a credential helper that can distinguish them.
On macOS, the osxkeychain helper stores credentials per URL, including the username:
On Linux, you can use the store helper with a custom file per account, or use the credential.useHttpPath option to differentiate by repository path:
This tells Git to store separate credentials for github.com/work-org/repo and github.com/personal/repo instead of sharing one credential for all of github.com.
GitHub Personal Access Tokens (PATs)
GitHub PATs replace passwords for HTTPS authentication. You can create fine-grained tokens scoped to specific repositories, which is ideal for multi-account setups.
Create a PAT at GitHub Settings then use it:
For automation or scripts, set the token in the remote URL:
Setting Per-Repo Author Identity
Cloning with the right account handles authentication, but you also need the correct author identity on commits. Set it per repository:
Or use conditional includes in ~/.gitconfig to automate this based on directory:
Then in ~/.gitconfig-work:
Common Pitfalls
- Forgetting
IdentitiesOnly yesin SSH config, which lets the SSH agent offer the wrong key and authenticate as the wrong account - Storing a PAT in the clone URL in your shell history, exposing it to anyone with access to the machine
- Setting
user.nameanduser.emailglobally instead of per-repo, causing commits to be attributed to the wrong account - Not enabling
credential.useHttpPathwhen using HTTPS with multiple accounts on the same host, causing credentials to collide - Cloning with HTTPS when your PAT has expired, then troubleshooting SSH config when the real problem is token rotation
Summary
- Embed the username in the HTTPS URL (
https://[email protected]/...) for quick one-off clones with a specific account - Use SSH host aliases in
~/.ssh/configwith separate keys for each account as the most reliable multi-account setup - Set
IdentitiesOnly yesin SSH config to prevent the agent from offering the wrong key - Use
GIT_SSH_COMMANDfor one-off clones without modifying SSH config - Enable
credential.useHttpPathwhen using HTTPS credential helpers with multiple accounts on the same host - Always set
user.nameanduser.emailper repository or use conditional includes to avoid mis-attributed commits

