You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the feature or problem you’d like to solve
I have two accounts on github each with access to a different set of private repositories. Currently I use a combination of git's [IncludeIf ...] config and a custom config which overrides the SSH private key I use for certain repositories. E.g.:
@eXamadeus GitHub CLI currently has no mechanism for switching between multiple GitHub accounts and I don't really have a workaround to suggest for you at this moment, sorry.
The only approach I could imagine, but would not recommend to anyone, would be to authenticate with 1st account, save a copy of ~/.config/gh/hosts.yml somewhere & delete the original file, authenticate again with the 2nd account, and now you can swap the ~/.config/gh/hosts.yml file with the backup file when you need to switch accounts.
Since this solution involves using SSH for git protocol, make sure both configuration files include the git_protocol: ssh line.
Reacted by Huy Z, hotoolong, DRogers, Julian Coy, Chris Clark, EVANDRO FRANCISCO GARCIA, ธีรพันธุ์ โอษฐ์จันทึก and Nadav Cohen
Reacted by EVANDRO FRANCISCO GARCIA and Nadav Cohen
If the cli can somehow sense which username the current repository is associated to. It would be easy to find the user in the hosts.yml and rest would be magic.
PS: I have not gone through the codebase. These are my assumptions.
Reacted by Mislav Marohnić, seed.of.apricot, tomholford, Zakery Clarke, Luis Paredes, Chris Perko, Toni DY, ShareForEver, Bruce Edge, Tedley M and 17 more
Searched and landed here. This feature would help people with two or more accounts (personal/business/others) and use both of them every day (swapping the config file would be too much for them).
Reacted by Benjamin Staneck, Ben Nelson-Weiss, Mat Pataki, tomholford, Zakery Clarke, p1va, Luis Paredes, Changsheng Wu, Gnyani, Sam and 57 more
It's a bit hacky, but I solved this so that I could evaluate if I want to actually use this cli by using the token auth capabilities, putting this into my dotfiles.
alias gh_cli=$(which gh)
function gh() {
if [[ $(pwd) =~ {PATH_TO_PERSONAL_CODE} ]]; then
GITHUB_TOKEN=$PERSONAL_GH_CLI_TOKEN
else
GITHUB_TOKEN=$WORK_GH_CLI_TOKEN
fi
GITHUB_TOKEN=$GITHUB_TOKEN gh_cli $@
unset GITHUB_TOKEN
}
Reacted by Prabhanjan, Daniel Tiziani, Changsheng Wu, Jeremy Wu, Stefano Charissis, Tristan Koch, Eugen Martynov, Prabhat Pandey, Toni DY, Julian Coy and 22 more
Since #2444 has been merged, as an alternative solution, set the environment variable GH_CONFIG_DIR to different directories could allow multiple accounts.
Reacted by Changsheng Wu, Leandro Lucarella (discontinued, see Bio), Dan, Adam Lindberg, Christopher Layne, Gary Miller, Iain Samuel McLean Elder, Víctor, John Bates, Ben Klein and 1 more
we use work credentials for all acme repositories and personal credentials for everything else.
Unfortunately, gh cli uses global settings for "default" host, so I postponed the implementation of gh auth status which uses several credentials at the same time.Implemented.
I am able to have multiple accounts signed in and that is nice, but one thing that I am trying to do is when I am creating a repository I would like to select my account. Doing something like gh repo create my_name/repo_name doesn't seem to work because one of my accounts is enterprise and one is public. Passing in my enterprise name like this causes HTTP 404: Not Found (https://api.github.com/users/my_enterprise_username) when I am logged into both my personal and enterprise GitHub but trying to create a repo for my private Github. This is not like a first-logged into account gets priority, no matter the order in which I log into my GitHub accounts I will receive this error when trying to create a repo for my enterprise account. Is there a way to force gh repo create to use the enterprise API instead of the public one? Right now I must log out of my personal account to create an enterprise repo unless I am doing something wrong.
I've tried a few different workarounds for this, and ended up just writing a little script and putting it on my path as gh, overriding the actual gh executable. This way, any other scripts/tools that call gh will first switch the user appropriately. This script sets the user based on the presence of a string in the current path, but you can customize the logic here however you want.
#!/usr/bin/env bash# Set the gh user based on the repo being accessedif [[ "$PWD"==*acme* ]];then# use myusername2 for acme repos
/usr/bin/gh auth switch -u myusername2 >/dev/null 2>&1else# otherwise use myusername1
/usr/bin/gh auth switch -u myusername1 >/dev/null 2>&1fi
/usr/bin/gh "$@"
gh auth switch --help
Switch the active account for a GitHub host.
This command changes the authentication configuration that will
be used when running commands targeting the specified GitHub host.
If the specified host has two accounts, the active account will be switched
automatically. If there are more than two accounts, disambiguation will be
required either through the --user flag or an interactive prompt.
For a list of authenticated accounts you can run gh auth status.
USAGE
gh auth switch [flags]
FLAGS
-h, --hostname string The hostname of the GitHub instance to switch account for
-u, --user string The account to switch to
INHERITED FLAGS
--help Show help for command
EXAMPLES
Select what host and account to switch to via a prompt
$ gh auth switch
Switch the active account on a specific host to a specific user
LEARN MORE
Use gh <command> <subcommand> --help for more information about a command.
Read the manual at https://cli.github.com/manual
Learn about exit codes using gh help exit-codes
I was able to login to multiple accounts using gh auth login and then switch between them with gh auth switch.
Interesting, though when I do this with a Github Enterprise account, switching to the github enterprise, it tells me its now the active account, but when i run gh repo list [owner] it does not pull from my org, and still pulls from github.com.
gh auth status
# output shows both accounts logged in one from enterprise url and one from github.com
gh auth switch--hostname github.xxx.xxx --user xxxxx
#outputs:# ✓ Switched active account for github.xxx.xx to xxxx
gh repo list
# outputs personal repos on github.com - the usernames are different as well, but i'm also specifiying the host in the switch.
Maybe something I'm still doing wrong but feels like this might be a potential issue with gh auth switch.
This command changes the authentication configuration that will
be used when running commands targeting the specified GitHub host.
You can target a host with GH_HOST, --hostname (for some commands), or it can be inferred from git remotes in your cwd.
From gh env:
`GH_HOST`: specify the GitHub hostname for commands where a hostname has not been provided, or
cannot be inferred from the context of a local Git repository. If this host was previously
authenticated with, the stored credentials will be used. Otherwise, setting `GH_TOKEN` or
`GH_ENTERPRISE_TOKEN` is required, depending on the targeted host.
FWIW I posted the first comment on this issue nearly 6 years ago. I wanted to loop back in to say my piece. It's been nice to see all the work the GH team has done to address feature requests and customer concerns. Seriously, yall are great.
I do believe the gh auth switch + some careful scripting would have solved my particular issue. It not fully automatic, but it did get around the awful "uninstall" loop that was the solution of the time.
BTW, I solved this about 5 years ago. I just moved to a company that let me use one Github account with SSO, so I no longer need to switch auth from personal to work 😆!
This command changes the authentication configuration that will
be used when running commands targeting the specified GitHub host.
You can target a host with GH_HOST, --hostname (for some commands), or it can be inferred from git remotes in your cwd.
From gh env:
`GH_HOST`: specify the GitHub hostname for commands where a hostname has not been provided, or
cannot be inferred from the context of a local Git repository. If this host was previously
authenticated with, the stored credentials will be used. Otherwise, setting `GH_TOKEN` or
`GH_ENTERPRISE_TOKEN` is required, depending on the targeted host.
This command changes the authentication configuration that will
be used when running commands targeting the specified GitHub host.
You can target a host with GH_HOST, --hostname (for some commands), or it can be inferred from git remotes in your cwd.
From gh env:
`GH_HOST`: specify the GitHub hostname for commands where a hostname has not been provided, or
cannot be inferred from the context of a local Git repository. If this host was previously
authenticated with, the stored credentials will be used. Otherwise, setting `GH_TOKEN` or
`GH_ENTERPRISE_TOKEN` is required, depending on the targeted host.
Hope this helps.
Yeah that's helpful thank you!
I guess I'm dense lol - I thought switching to the account gave it the inference it needed using gh auth switch but I see what you mean now.
I'm switching to GH CLI (was using GitKraken for a long time, but due to budget we are no longer using it).
From some testing, it seems if I want to do tasks like gh repo list ill just have to set that env var up. No biggie I can easily write wrapper commands to do that.
I guess I'm dense lol - I thought switching to the account gave it the inference it needed using gh auth switch but I see what you mean now.
Pretty understandable tbh, I don't think you're the first person maybe even in this thread that has hit the same issue. Part of the problem is that multi account came long after host targeting so the worlds conflict a little bit. The env var is your best bet for gh repo list indeed.
I've tried a few different workarounds for this, and ended up just writing a little script and putting it on my path as gh, overriding the actual gh executable. This way, any other scripts/tools that call gh will first switch the user appropriately. This script sets the user based on the presence of a string in the current path, but you can customize the logic here however you want.
I took this idea and made a version (in Fish — sorry!) that does the same but switches based on the configured Git user.name which is expected to match a/your GitHub account login username. For me, I have my Git config based around includeIf for certain directories, so I just have to set that for my GitHub account to also match my configured Git user.
function gh
set user $(git config user.name)
ifcommand gh auth status--json hosts --jq'.hosts["github.com"].[].login'|grep-Fqx"$user"command gh auth switch-u"$user">/dev/null 2>&1elseecho"gh: matching gh user for git user '$user' not found"endcommand gh $argvend
Hope this helps :)
Reacted by @tobeycodes, ArNazXx and Emily Marigold Klassen
I came at this problem from a different direction, looking to make it automatic to use my non-personal account for any repo in the non-personal org, and my personal account for everything else. I implemented it in a PR, that was of course automatically closed, but wanted to add it here as a reference as well.
I set it up so that you can add a mapping of owner -> user, and then any repo with a configured owner will use the mapped user via the factory created http client. If there is not an owner->user match, it will use the globally active one. This allows me to leave it as my personal account in general, but ensures that when I need to interact with a non-personal repo, I'm using the correct credential.
The solutions suggested here are good solutions if the repos are segregated by directory easily. That wasn't the case for me, so the owner path seemed the most straightforward.
It maps folders to gh accounts (~/.config/ghx/config.yaml, deepest match wins) or a per-repo .ghx file, then runs the real gh with GH_TOKEN from gh auth token --user <login>. It never calls gh auth switch, so terminals and agents in different folders don't change each other's account. All gh commands, flags and extensions pass through unchanged. It installs as a gh shim, and gh auth git-credential gets the folder's token, so git push uses the right account too.
Activity
eXamadeus commentedon May 4, 2020
Is there currently a way to work around this by removing the current authentication? I can't seem to find it.
Reinstalling seems to work, but it's very cumbersome.
mislav commentedon May 4, 2020
@eXamadeus GitHub CLI currently has no mechanism for switching between multiple GitHub accounts and I don't really have a workaround to suggest for you at this moment, sorry.
The only approach I could imagine, but would not recommend to anyone, would be to authenticate with 1st account, save a copy of
~/.config/gh/hosts.ymlsomewhere & delete the original file, authenticate again with the 2nd account, and now you can swap the~/.config/gh/hosts.ymlfile with the backup file when you need to switch accounts.Since this solution involves using SSH for git protocol, make sure both configuration files include the
git_protocol: sshline.aitchkhan commentedon Sep 18, 2020
If we can add two entries or as many entries on every
gh auth login. It might become easier with something like this:If the
clican somehow sense which username the current repository is associated to. It would be easy to find the user in the hosts.yml and rest would be magic.PS: I have not gone through the codebase. These are my assumptions.
seed-of-apricot commentedon Sep 30, 2020
Searched and landed here. This feature would help people with two or more accounts (personal/business/others) and use both of them every day (swapping the config file would be too much for them).
fdm1 commentedon Sep 30, 2020
It's a bit hacky, but I solved this so that I could evaluate if I want to actually use this cli by using the token auth capabilities, putting this into my dotfiles.
seed-of-apricot commentedon Oct 1, 2020
@fdm1 Which config file did you fill that in?
fdm1 commentedon Oct 1, 2020
Just in my bash dotfiles
tomholford commentedon Dec 15, 2020
Now that #2179 has been merged, perhaps it will be less effort to implement this feature?
Would be nice to have something like how
githandles it:~/.gitconfig~/.gitconfig.workedit
Now that I think about it some more, can
ghinfer which authed account to use from the currentgit config user.nameor email?chaoticlonghair commentedon Feb 4, 2021
Since #2444 has been merged, as an alternative solution, set the environment variable
GH_CONFIG_DIRto different directories could allow multiple accounts.dantonyuk commentedon Mar 22, 2021
I just implemented simple multi-account support based on the
remote.origin.urlconfig property (of course GH_HOST is on board as well): #3278So having hosts.yml as
we use work credentials for all acme repositories and personal credentials for everything else.
Unfortunately, gh cli uses global settings for "default" host, so I postponed the implementation ofImplemented.gh auth statuswhich uses several credentials at the same time.jeremy-ww commentedon Mar 31, 2021
Maybe this is a workaround if u wanna use multi credentials for a specific command. such as
pr view/usr/local/pr2
~/.pr-config.json
{ "[email protected]": { "host": "enterprise.com", "token": "" }, "[email protected]": { "host": "enterprise1.com", "token": "" } }thecharlesjenkins commentedon Apr 13, 2021
I am able to have multiple accounts signed in and that is nice, but one thing that I am trying to do is when I am creating a repository I would like to select my account. Doing something like
gh repo create my_name/repo_namedoesn't seem to work because one of my accounts is enterprise and one is public. Passing in my enterprise name like this causesHTTP 404: Not Found (https://api.github.com/users/my_enterprise_username)when I am logged into both my personal and enterprise GitHub but trying to create a repo for my private Github. This is not like a first-logged into account gets priority, no matter the order in which I log into my GitHub accounts I will receive this error when trying to create a repo for my enterprise account. Is there a way to forcegh repo createto use the enterprise API instead of the public one? Right now I must log out of my personal account to create an enterprise repo unless I am doing something wrong.102 remaining items
MiK35402 commentedon Jul 22, 2025
MSch commentedon Jul 26, 2025
FYI I vibecoded a POC that already works for my needs: #11388
drmercer-lucid commentedon Jan 5, 2026
I've tried a few different workarounds for this, and ended up just writing a little script and putting it on my path as
gh, overriding the actualghexecutable. This way, any other scripts/tools that callghwill first switch the user appropriately. This script sets the user based on the presence of a string in the current path, but you can customize the logic here however you want.dotnvo commentedon Feb 24, 2026
Interesting, though when I do this with a Github Enterprise account, switching to the github enterprise, it tells me its now the active account, but when i run
gh repo list [owner]it does not pull from my org, and still pulls from github.com.Maybe something I'm still doing wrong but feels like this might be a potential issue with
gh auth switch.williammartin commentedon Feb 24, 2026
@dotnvo per
gh auth switchhelp:You can target a host with
GH_HOST,--hostname(for some commands), or it can be inferred from git remotes in your cwd.From
gh env:Hope this helps.
eXamadeus commentedon Feb 24, 2026
FWIW I posted the first comment on this issue nearly 6 years ago. I wanted to loop back in to say my piece. It's been nice to see all the work the GH team has done to address feature requests and customer concerns. Seriously, yall are great.
I do believe the
gh auth switch+ some careful scripting would have solved my particular issue. It not fully automatic, but it did get around the awful "uninstall" loop that was the solution of the time.BTW, I solved this about 5 years ago. I just moved to a company that let me use one Github account with SSO, so I no longer need to switch auth from personal to work 😆!
dotnvo commentedon Feb 24, 2026
Yeah that's helpful thank you!
I guess I'm dense lol - I thought switching to the account gave it the inference it needed using
gh auth switchbut I see what you mean now.I'm switching to GH CLI (was using GitKraken for a long time, but due to budget we are no longer using it).
From some testing, it seems if I want to do tasks like
gh repo listill just have to set that env var up. No biggie I can easily write wrapper commands to do that.williammartin commentedon Feb 24, 2026
Pretty understandable tbh, I don't think you're the first person maybe even in this thread that has hit the same issue. Part of the problem is that multi account came long after host targeting so the worlds conflict a little bit. The env var is your best bet for
gh repo listindeed.uncenter commentedon Feb 25, 2026
I took this idea and made a version (in Fish — sorry!) that does the same but switches based on the configured Git
user.namewhich is expected to match a/your GitHub account login username. For me, I have my Git config based aroundincludeIffor certain directories, so I just have to set that for my GitHub account to also match my configured Git user.Hope this helps :)
caseycs commentedon Apr 9, 2026
I came up with the workaround for OSX using Seatbelt: https://github.com/caseycs/gh-sandbox-osx
docs: add motivation section referencing cli/cli#326
galamdring commentedon Apr 10, 2026
I came at this problem from a different direction, looking to make it automatic to use my non-personal account for any repo in the non-personal org, and my personal account for everything else. I implemented it in a PR, that was of course automatically closed, but wanted to add it here as a reference as well.
I set it up so that you can add a mapping of owner -> user, and then any repo with a configured owner will use the mapped user via the factory created http client. If there is not an owner->user match, it will use the globally active one. This allows me to leave it as my personal account in general, but ensures that when I need to interact with a non-personal repo, I'm using the correct credential.
The solutions suggested here are good solutions if the repos are segregated by directory easily. That wasn't the case for me, so the owner path seemed the most straightforward.
PR: #13130
Root Cause Analysis: #12885
Similar Issues:
#12145
#9111
#11938
barnuri commentedon Oct 15, 2026
Until this lands natively, I built a small wrapper that does this: https://github.com/barnuri/ghx
It maps folders to gh accounts (
~/.config/ghx/config.yaml, deepest match wins) or a per-repo.ghxfile, then runs the realghwithGH_TOKENfromgh auth token --user <login>. It never callsgh auth switch, so terminals and agents in different folders don't change each other's account. All gh commands, flags and extensions pass through unchanged. It installs as aghshim, andgh auth git-credentialgets the folder's token, sogit pushuses the right account too.