Description
Describe the bug
I am using gh CLI on a Macbook with Sonoma 14.3. I would like to use gh auth switch to switch between my private user ("arthurhobspice") and my company user ("Christoph-Bauer_voda"), usable only in a closed Enterprise context. However, the user switch does not have the expected effect.
> gh version 2.43.1 (2024-01-31)
https://github.com/cli/cli/releases/tag/v2.43.1
> git version
git version 2.39.3 (Apple Git-145)
Steps to reproduce the behavior
Create a private repository under one of the accounts, for example the private one ("arthurhobspice"). Repo name: arthur-test.
Log in as both users using two consecutive gh auth login, first as the private one, then as the company user:
> gh auth login --hostname github.com
? What is your preferred protocol for Git operations on this host? HTTPS
? Authenticate Git with your GitHub credentials? Yes
? How would you like to authenticate GitHub CLI? Paste an authentication token
Tip: you can generate a Personal Access Token here https://github.com/settings/tokens
The minimum required scopes are 'repo', 'read:org', 'workflow'.
? Paste your authentication token: ****************************************
- gh config set -h github.com git_protocol https
✓ Configured git protocol
✓ Logged in as arthurhobspice
> gh auth login --hostname github.com
? What is your preferred protocol for Git operations on this host? HTTPS
? Authenticate Git with your GitHub credentials? Yes
? How would you like to authenticate GitHub CLI? Paste an authentication token
Tip: you can generate a Personal Access Token here https://github.com/settings/tokens
The minimum required scopes are 'repo', 'read:org', 'workflow'.
? Paste your authentication token: ****************************************
- gh config set -h github.com git_protocol https
✓ Configured git protocol
✓ Logged in as Christoph-Bauer_voda
Works as expected: the company user is the active one:
> gh auth status
github.com
✓ Logged in to github.com account Christoph-Bauer_voda (keyring)
- Active account: true
- Git operations protocol: https
- Token: ghp_************************************
- Token scopes: 'read:org', 'repo', 'workflow'
✓ Logged in to github.com account arthurhobspice (keyring)
- Active account: false
- Git operations protocol: https
- Token: gho_************************************
- Token scopes: 'gist', 'read:org', 'repo', 'workflow'
Go to the local clone of arthur-test and run e.g. a git pull. Since the repository is private and owned by the user "arthurhobspice", this should fail, and it does. Run gh auth switch to switch the active user and repeat the git pull operation. This should work. But it doesn't:
> cd arthur-test
> git pull
remote: Repository not found.
fatal: repository 'https://github.com/arthurhobspice/arthur-test.git/' not found
> gh auth switch
? What account do you want to switch to? arthurhobspice (github.com)
✓ Switched active account for github.com to arthurhobspice
> gh auth status
github.com
✓ Logged in to github.com account arthurhobspice (keyring)
- Active account: true
- Git operations protocol: https
- Token: gho_************************************
- Token scopes: 'gist', 'read:org', 'repo', 'workflow'
✓ Logged in to github.com account Christoph-Bauer_voda (keyring)
- Active account: false
- Git operations protocol: https
- Token: ghp_************************************
- Token scopes: 'read:org', 'repo', 'workflow'
> git pull
remote: Repository not found.
fatal: repository 'https://github.com/arthurhobspice/arthur-test.git/' not found
Expected vs actual behavior
gh auth switch should allow GitHub API operations under both users by actually switching the active user. But it seems that the user that has first been logged in remains active ("internally") even after running gh auth switch.
Activating the private user ("arthurhobspice") in the example above is only possible by calling gh auth logout for both users and then gh auth login for "arthurhobspice" only:
> gh auth logout --hostname github.com
? What account do you want to log out of? arthurhobspice (github.com)
✓ Logged out of github.com account arthurhobspice
✓ Switched active account for github.com to Christoph-Bauer_voda
> gh auth logout --hostname github.com
✓ Logged out of github.com account Christoph-Bauer_voda
> gh auth login --hostname github.com
? What is your preferred protocol for Git operations on this host? HTTPS
? Authenticate Git with your GitHub credentials? Yes
? How would you like to authenticate GitHub CLI? Paste an authentication token
Tip: you can generate a Personal Access Token here https://github.com/settings/tokens
The minimum required scopes are 'repo', 'read:org', 'workflow'.
? Paste your authentication token: ****************************************
- gh config set -h github.com git_protocol https
✓ Configured git protocol
✓ Logged in as arthurhobspice
> git pull
Already up to date.
git config
> git config -l
credential.helper=osxkeychain
init.defaultbranch=main
credential.https://github.com/.helper=!/opt/homebrew/bin/gh auth git-credential
user.name=Christoph Bauer
[email protected]
includeif.gitdir:~/Git/gh-vf/.path=~/.gitconfig.vf
merge.ff=only
branch.autosetuprebase=always
branch.autosetupmerge=always
core.autocrlf=input
alias.co=checkout
alias.cleanup=!git remote prune origin && git gc && git clean -df && git stash clear
alias.s=status
push.default=current
push.autosetupremote=true
init.defaultbranch=main
pull.ff=only
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
remote.origin.url=https://github.com/arthurhobspice/arthur-test.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.main.remote=origin
branch.main.merge=refs/heads/main
branch.main.rebase=true
(Just to be mentioned: the includeif.gitdir setting uses a separate config file to change user.email to the company email address for all git operations in repository clones under ~/Git/gh-vf. Should not have an impact on this issue.)
Activity
andyfeller commentedon Feb 9, 2024
@arthurhobspice : thanks for opening this issue and personal apologies it is not working as desired! 🙇
In the original multi account work, my colleague @williammartin captured a number of use cases that fell short of the original work within the "Multiple Accounts with the CLI - v2.40.0" documentation which includes
gituse cases like this:Though these were intentional at the time so we could ship the feature, we knew these would be capabilities our users would desire understandably. To that end, I need to double check whether subsequent issues were created for those additional features; if so, then we can close this as a duplicate, otherwise keep it open and rework to be a new feature / enhancement.
[-]`gh auth switch` on MacOS does not work as expected[/-][+]Extend `gh auth switch` to affect `git` configuration on a repository[/+]arthurhobspice commentedon Feb 9, 2024
Thanks a lot @andyfeller for the quick reply. I just tested the same on Linux (Ubuntu 23.10), and it works fine and as expected, I can switch between the two users there and interact with their respective repos without any issue. Seems to be an issue specific to the git and/or gh CLI on MacOS. Maybe related to the git setting
credential.helper=osxkeychain? (I don't see where to change that, it's neither in local nor in global settings.)Working Linux command versions:
Hence I would still call it a "bug", but a MacOS specific one.
williammartin commentedon Feb 9, 2024
I would expect this to work. Running
gh auth switchshould switch the token that is returned byghwhen used as a credential manager. I suspect what's happening here is that on your mac you haven't configuredgitto useghas the credential manager. In yourgit config -loutput I would expect to see something like:Perhaps you can check that this exists on Linux? In either case, I would suggest you run
gh auth setup-gitand try your flow again.Thank you for your detailed bug report.
williammartin commentedon Feb 9, 2024
I am slightly confused why this didn't happen with regards to the line:
Perhaps when you log in you could include the output
gh auth loginandgh auth setup-gitwithGH_DEBUG=1set. I would expect forsetup-gitthat it looks something like:thomhurst commentedon Feb 9, 2024
I have an issue switching between GitHub accounts which have access to private repositories too.
I can auth switch, but the moment I try to clone, checkout, push etc I get repository not found.
I have to do another gh Auth login to I guess refresh my credentials? It works after that.
gh then tells me that I was already logged in, even though I couldn't do anything with that account.
thomhurst commentedon Feb 9, 2024
Listing my git config as mentioned above, I don't have an entry for
credential.https://github.com.helper
But I do have:
credential.helper=manager
Is this the reason?
williammartin commentedon Feb 9, 2024
@thomhurst please provide the output of:
and try your git operations again.
thomhurst commentedon Feb 9, 2024
I've just run gh auth setup-git and that seems to have fixed my problem!
arthurhobspice commentedon Feb 13, 2024
@thomhurst what you describe looks like my issue. But I guess you are on Windows? (
credential.helper=manager)@williammartin thanks for the suggested commands. Here's my output:
With these settings, pulling with both users should work. Alas, it doesn't:
The only working user is the one that has done
gh auth loginlast. When I dogh auth loginwith the other user, I can pull from the other user's repo but not from the first one anymore. Here's whatgit config -lreports about the credential helpers:Does the
osxkeychainhelper interfere maybe?mosheduminer commentedon Feb 13, 2024
I want to add a datapoint, I am on windows and faced the problem mentioned above, that I was unable to access repositories for one of my accounts (even though it was active) with multiple accounts logged in. I found this issue thread and ran
gh auth setup-gitas suggested by @thomhurst, and that has solved the issue.arthurhobspice commentedon Feb 14, 2024
In my last comment, I stupidly missed the important point: running
gh auth switchbetween pulling from different repositories. That's what this story is about, isn't it? Indeed, everything works fine aftergh auth setup-git. I suspect that this section in my original.gitconfig(which was not removed by
gh auth setup-git, I had to remove it manually afterwards) was the culprit - with the additional/character after the host name it is surely misspelled.Thank you @williammartin for pointing me to
gh auth setup-gitand sorry for the inconvenience, this issue can be closed.williammartin commentedon Feb 14, 2024
No problem @arthurhobspice, I'm still a little suspicious why this wasn't handled automatically during your original
gh auth loginin this step:But I think without a bit more information it'll probably be challenging to figure out. Gonna close this out and if we experience it again will ask for more debugging up front. Cheers everyone for chiming in.
blindwiz commentedon Feb 23, 2024
So I'm on windows 11, with all latest updates. I have gh v2.44.1 (2024-02-16)
when I type gh auth switch, it sswitches my account, but git config --global user.email stays the same. I made sure my credential option is set to gh and it is.
Am I wrong in that gh auth switch, or gh auth login isn't supposed to set the gh config --global user.name and user.email settings?
williammartin commentedon Feb 23, 2024
ghdoesn't touch theuser.nameoruser.emailSee #8590 (comment) for further discussion.
RoyLeviGit commentedon Mar 25, 2024
I would like to wake up this thread again as I agree as said above that
gh auth loginshould do the setup done bygh auth setup-gitthat solves this issue.I believe otherwise this thread would keep coming up for people with the same issue just like it did for me :)
williammartin commentedon Mar 25, 2024
@RoyLeviGit thanks for chiming in. Your message prompted me to have another look at #8678 (comment) which gave me fresh perspective. I'm definitely happy to have a further conversation on how to improve this on a new issue but I'd prefer to keep this one closed to avoid pinging people and to keep the conversation focused.
I've created #8875.
MilanObrenovic commentedon Jul 8, 2025
this fixed it for me
AnilGupte commentedon Sep 4, 2025
I am on Windows and had the same problem as OP. I ran "gh auth setup-git" (there's no output unfortunately, but...), it solved the issue!
williammartin commentedon Sep 4, 2025
Yeh the lack of output is tracked by #11191, thanks for chiming in.
chi11ax commentedon Oct 29, 2025
Had this issue today too. I didn't get to use
gh auth setup-gitas I logged out of both accounts to solve my problem before I found this. Do I need to rungh auth setup-gitevery time I switch or just once?williammartin commentedon Oct 29, 2025
gh auth switchonly needs to be run once, it configuresghas a credential helper in you~/.gitconfig. What this means is that whengitis working with a repo that has a remote ongithub.com(or whatever hosts you had logged in as when you rungh auth switch), it will askghfor a token.