Repository navigation
Open
Labels
authrelated to tokens, authentication state, or oauthcoreThis issue is not accepting PRs from outside contributorsenhancementa request to improve CLIpitchpitched internally for prioritisation
Description
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.:
In ~/.gitconfig:
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig
and in ~/work/.gitconfig:
[core]
sshCommand = "ssh -i ~/.ssh/work"
It will be great if the credentials for gh can also be configured per repository as well.
Proposed solution
There are a bunch of options I can think of:
- Allow overriding
ghconfig by a file in a local clone's.gitdirectory - Allow defining rules in the
ghconfig which defines which repos/orgs should use which credential. E.g.
github.com/google:
- user: gemployee
oauth_token: ...
github.com/microsoft:
- user: msemployee
oauth_token: ...
github.com:
- user: personal
oauth_token: ...
How will it benefit CLI and its users?
It will allow Github users with multiple accounts of varying access to seamlessly work on different repositories.
Reacted by Vedran Karačić, geeseven, balloonio, Benjamin Staneck, Frank Massi, Brian Macauley, Julian Coy, DCM, Benjamin Staneck, Evgeniy Timokhov and 349 more
Reacted by Benjamin Golinvaux, lilman9716, Nadav Cohen, jtrv and tops-jayendrasinh
Reacted by Ennel-Foods, Santiago Toscanini, alexisromagnoli-halo, Utku Demir, siva, Hassan Tabatabaie, Dev Ganesh, DaTsik99, Intellectual777, Hayk Baghdasaryan and 10 more
Reacted by B, Julian Coy, Helmut K. C. Tessarek, Gui Vieira, Colton Donnelly, salpreh, Rohit Sharma, Mohit Raj, Luis Paredes, Ty Mick and 61 more
Reacted by Toni DY, benyapa1493, Limon Monte, Oliver Mannion, Ethan Schoen, Ryota Bannai, Matheus Polachini, Marcelo Kochiyama, Ennel-Foods, Santiago Toscanini and 21 more
Reacted by Will, Tedley M, Oliver Mannion, Kannan Kumar, Matheus Polachini, Santiago Toscanini, hovak, Saberhabeel, Utku Demir, Emidavid15 and 12 more
Metadata
Metadata
Assignees
Labels
authrelated to tokens, authentication state, or oauthcoreThis issue is not accepting PRs from outside contributorsenhancementa request to improve CLIpitchpitched internally for prioritisation
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.103 remaining items
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.us-urvin commentedon Nov 20, 2026
For anyone who needs per-directory account selection today, I built an open-source workaround (disclosure: it's my project) while this is still open.
It picks the account by folder, without switching anything globally:
includeIf "gitdir/i:<folder>/"loads a per-account config: commit name/email, plus a credential helper that runsgh auth token --user <name>for HTTPS (orcore.sshCommandwith that account's key for SSH)claudewrapper (for Claude Code) setsGH_TOKENper process, sogh pr createinside that session uses the folder's account and parallel sessions in other folders aren't affectedOne-line installers for macOS, Linux and Windows: https://github.com/us-urvin/claude-gh-accounts
Feedback welcome, especially if a native
ghsolution lands and makes parts of it redundant.