ThisCalculator

Everyday

Multiple GitHub Accounts: gh CLI PAT by Repo Owner

Updated · Standard formula, shown below

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 repo scope for private repositories, or public_repo for public only.
  • Access to a terminal (Command Prompt, PowerShell, or bash).

Step-by-Step

  1. Authenticate gh with your first account. Run gh auth login and follow the prompts. Choose GitHub.com, then HTTPS, then authenticate via browser or paste a token. This becomes your default account.
  2. Authenticate gh with your second account. Run gh auth login --hostname github.com --git-protocol https --web again, but this time choose the second account when prompted. gh stores both accounts and asks which to use for new operations.
  3. Verify both accounts are known. Run gh auth status. You should see both accounts listed. Note the exact account names (e.g., personal-user and work-user).
  4. Set up Git to use gh as a credential helper. Run gh auth setup-git. This configures Git to ask gh for credentials when pushing or pulling.
  5. Create a Git config include for account-specific settings. Open (or create) the file ~/.gitconfig (or C:\Users\YourName\.gitconfig on Windows) in a text editor. Add these lines:
    [includeIf "gitdir:~/work/"]
        path = ~/.gitconfig-work
    

    This 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/).

  6. Create the included config file. Create ~/.gitconfig-work with:
    [user]
        name = Your Work Name
        email = you@work.com
    [credential "https://github.com"]
        username = work-user
    

    Replace the name, email, and username with your work account details.

  7. Repeat for your personal account. Add another includeIf block in ~/.gitconfig for your personal repositories, e.g., gitdir:~/personal/ pointing to ~/.gitconfig-personal. In that file, set your personal name, email, and username.
  8. 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.
  9. Test the setup. In a work repo, run git config user.name and git config credential.username to confirm they show work values. Then push a test commit. Git will ask gh for a token; gh will use the account that matches the username in the credential helper.
  10. Ensure gh switches accounts automatically. If you have multiple accounts authenticated, gh will 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 adding username = work-user as 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, gh might pick the wrong account. Always set credential.username in 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 ask gh for credentials and will fall back to your default Git credentials.
  • Editing the global config incorrectly. A syntax error in ~/.gitconfig can break all Git commands. Always test with git config --list after 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 switch to 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 gh is 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-run git config --unset-all credential.username in the repo, then push again.
  • Git asks for a username/password instead of using gh. Run gh auth setup-git again, and make sure the credential helper is set: git config --global credential.helper should 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 include to verify the includeIf lines are present.
  • Token has expired. GitHub tokens can expire. Re-authenticate with gh auth refresh -h github.com -s repo to 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.