Multiple GitHub Accounts: gh CLI PAT by Repo Owner
This guide shows you how to use the GitHub CLI (gh) with multiple GitHub accounts on the same machine, automatically selecting the right personal access token (PAT) based on the repository owner. This is essential for developers who manage both personal and work repositories without constantly logging in and out. You’ll set up a Git configuration that tells gh which token to use for which repository owner, so pushes and pulls just work.
What You’ll Need
- Git installed (version 2.13 or later).
- GitHub CLI (
gh) installed and authenticated with at least one account. - Personal access tokens (PATs) for each GitHub account you want to use. Create them in GitHub Settings → Developer settings → Personal access tokens (classic). Give each token the
reposcope for private repositories, orpublic_repofor public only. - Access to a terminal (Command Prompt, PowerShell, or bash).
Step-by-Step
- Authenticate
ghwith your first account. Rungh auth loginand follow the prompts. Choose GitHub.com, then HTTPS, then authenticate via browser or paste a token. This becomes your default account. - Authenticate
ghwith your second account. Rungh auth login --hostname github.com --git-protocol https --webagain, but this time choose the second account when prompted.ghstores both accounts and asks which to use for new operations. - Verify both accounts are known. Run
gh auth status. You should see both accounts listed. Note the exact account names (e.g.,personal-userandwork-user). - Set up Git to use
ghas a credential helper. Rungh auth setup-git. This configures Git to askghfor credentials when pushing or pulling. - Create a Git config include for account-specific settings. Open (or create) the file
~/.gitconfig(orC:\Users\YourName\.gitconfigon Windows) in a text editor. Add these lines:[includeIf "gitdir:~/work/"] path = ~/.gitconfig-workThis tells Git: for any repository under
~/work/, apply the settings from~/.gitconfig-work. Adjust the path to match where you keep work repositories (e.g.,~/projects/work/). - Create the included config file. Create
~/.gitconfig-workwith:[user] name = Your Work Name email = you@work.com [credential "https://github.com"] username = work-userReplace the name, email, and username with your work account details.
- Repeat for your personal account. Add another
includeIfblock in~/.gitconfigfor your personal repositories, e.g.,gitdir:~/personal/pointing to~/.gitconfig-personal. In that file, set your personal name, email, and username. - Clone or move repositories into the matching directories. For example, clone work repos under
~/work/and personal repos under~/personal/. Git will automatically pick the right config based on the path. - Test the setup. In a work repo, run
git config user.nameandgit config credential.usernameto confirm they show work values. Then push a test commit. Git will askghfor a token;ghwill use the account that matches the username in the credential helper. - Ensure
ghswitches accounts automatically. If you have multiple accounts authenticated,ghwill use the one that has a token with access to the repository. If it doesn’t, you may need to specify the account in the credential helper by addingusername = work-useras shown.
Common Mistakes
- Using the same token for both accounts. Each account must have its own token. Sharing tokens can cause permission errors.
- Not setting the credential username. Without it,
ghmight pick the wrong account. Always setcredential.usernamein the include file. - Putting repositories in the wrong folder. If you clone a work repo into your personal folder, Git will use your personal config and the push will fail or go to the wrong account.
- Forgetting to run
gh auth setup-git. Without this, Git won’t askghfor credentials and will fall back to your default Git credentials. - Editing the global config incorrectly. A syntax error in
~/.gitconfigcan break all Git commands. Always test withgit config --listafter editing.
Tips
- Use a clear directory structure:
~/work/and~/personal/. This makes the includeIf rules obvious. - If you have more than two accounts, add more includeIf blocks with unique paths.
- You can also use
gh auth switchto manually switch the default account, but the includeIf method is fully automatic. - Store your PATs securely. Consider using a password manager. Never commit tokens to a repository.
- For SSH instead of HTTPS, you would configure SSH keys per account, but this guide focuses on PATs.
Troubleshooting
- Push fails with “Permission to repo denied.” This means
ghis using the wrong token. Check the credential username in the repo:git config credential.username. If it’s wrong, edit the include file and re-rungit config --unset-all credential.usernamein the repo, then push again. - Git asks for a username/password instead of using
gh. Rungh auth setup-gitagain, and make sure the credential helper is set:git config --global credential.helpershould show!/usr/bin/gh auth git-credential(or similar). - Both accounts have the same username. That’s not possible on GitHub; usernames are unique. If you’re using the same username, you’re using the same account.
- Changes to .gitconfig don’t take effect. Git reads config at command start. Close and reopen your terminal, or run
git config --global --get-regexp includeto verify the includeIf lines are present. - Token has expired. GitHub tokens can expire. Re-authenticate with
gh auth refresh -h github.com -s repoto get a new token.
Frequently Asked Questions
Can I use this method with SSH instead of HTTPS?
No, this guide is for HTTPS with PATs. For SSH, you’d configure multiple SSH keys and set core.sshCommand per repo.
What if I have repositories from both accounts in the same folder?
Use separate folders for each account. If you must mix, you can set the config manually in each repo with git config user.name and git config credential.username, but it’s easier to keep them separate.
Do I need to create a new PAT for each account?
Yes, each GitHub account has its own tokens. You can’t use a token from one account to authenticate as another.
How do I add a third account?
Authenticate it with gh auth login, then add another includeIf block in ~/.gitconfig pointing to a new config file with that account’s details.
Will this affect my global Git settings like editor or aliases?
No, the includeIf files only override the settings you specify (user, credential). Global settings still apply to all repositories.
Why does gh auth status show only one account?
If you authenticated with the same browser session, gh might have overwritten. Use gh auth login again and choose “Add account” when prompted.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.