Skip to content

Extend gh auth switch to affect git configuration on a repository #8678

Closed

Description

@arthurhobspice

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

  1. added
    bug
    on Feb 8, 2024
  2. added
    needs-triage
    on Feb 8, 2024
  3. andyfeller commented on Feb 9, 2024

    @andyfeller
    Contributor

    @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 git use cases like this:

    What is out of scope for this release?

    As mentioned above, we know that this only addresses some of the requests around supporting multiple accounts. While these are not out of scope forever, for this release some of the big things we have intentionally not included are:

    • Automatic account switching based on some context (e.g. pwd, git remote, etc)
    • Automatic configuration of git config such as user.name and user.email when switching
    • User level configuration e.g. williammartin uses vim but wilmartin_microsoft uses emacs

    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.

  4. added
    enhancement
    gh-auth
    and removed
    bug
    needs-triage
    on Feb 9, 2024
  5. changed the title [-]`gh auth switch` on MacOS does not work as expected[/-] [+]Extend `gh auth switch` to affect `git` configuration on a repository[/+] on Feb 9, 2024
  6. arthurhobspice commented on Feb 9, 2024

    @arthurhobspice
    Author

    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:

    
    > git --version
    git version 2.40.1
    
    > gh --version  
    gh version 2.42.1 (2024-01-16)
    https://github.com/cli/cli/releases/tag/v2.42.1
    

    Hence I would still call it a "bug", but a MacOS specific one.

  7. williammartin commented on Feb 9, 2024

    @williammartin
    Member

    I would expect this to work. Running gh auth switch should switch the token that is returned by gh when used as a credential manager. I suspect what's happening here is that on your mac you haven't configured git to use gh as the credential manager. In your git config -l output I would expect to see something like:

    credential.https://github.com.helper=
    credential.https://github.com.helper=!/opt/homebrew/bin/gh auth git-credential
    

    Perhaps you can check that this exists on Linux? In either case, I would suggest you run gh auth setup-git and try your flow again.

    Thank you for your detailed bug report.

  8. added
    more-info-needed
    on Feb 9, 2024
  9. williammartin commented on Feb 9, 2024

    @williammartin
    Member

    I am slightly confused why this didn't happen with regards to the line:

    ? Authenticate Git with your GitHub credentials? Yes
    

    Perhaps when you log in you could include the output gh auth login and gh auth setup-git with GH_DEBUG=1 set. I would expect for setup-git that it looks something like:

    ➜  GH_DEBUG=1 gh auth setup-git
    [git config --global --replace-all credential.https://github.com.helper ]
    [git config --global --add credential.https://github.com.helper !/opt/homebrew/bin/gh auth git-credential]
    [git config --global --replace-all credential.https://gist.github.com.helper ]
    [git config --global --add credential.https://gist.github.com.helper !/opt/homebrew/bin/gh auth git-credential]
    
  10. thomhurst commented on Feb 9, 2024

    @thomhurst

    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.

  11. thomhurst commented on Feb 9, 2024

    @thomhurst

    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?

  12. williammartin commented on Feb 9, 2024

    @williammartin
    Member

    @thomhurst please provide the output of:

    GH_DEBUG=1 gh auth setup-git
    

    and try your git operations again.

  13. thomhurst commented on Feb 9, 2024

    @thomhurst

    I've just run gh auth setup-git and that seems to have fixed my problem!

  14. arthurhobspice commented on Feb 13, 2024

    @arthurhobspice
    Author

    @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:

    > GH_DEBUG=1 gh auth setup-git           
    [git config --global --replace-all credential.https://github.com.helper ]
    [git config --global --add credential.https://github.com.helper !/opt/homebrew/bin/gh auth git-credential]
    [git config --global --replace-all credential.https://gist.github.com.helper ]
    [git config --global --add credential.https://gist.github.com.helper !/opt/homebrew/bin/gh auth git-credential]
    
    > GH_DEBUG=1 gh auth login --hostname github.com
    ? What is your preferred protocol for Git operations on this host? HTTPS
    [git config credential.https://github.com.helper]
    ? 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: ****************************************
    * Request at 2024-02-13 08:07:19.214901 +0100 CET m=+23.490134293
    * Request to https://api.github.com/
    * Request took 518.898208ms
    * Request at 2024-02-13 08:07:19.734301 +0100 CET m=+24.009538668
    * Request to https://api.github.com/graphql
    * Request took 274.158541ms
    - gh config set -h github.com git_protocol https
    ✓ Configured git protocol
    ✓ Logged in as Christoph-Bauer_voda
    
    > GH_DEBUG=1 gh auth status                     
    * Request at 2024-02-13 08:12:32.841139 +0100 CET m=+0.088206376
    * Request to https://api.github.com/
    * Request took 522.966541ms
    * Request at 2024-02-13 08:12:33.399637 +0100 CET m=+0.646710334
    * Request to https://api.github.com/
    * Request took 229.151833ms
    github.com
      ✓ Logged in to github.com account Christoph-Bauer_voda (keyring)
      - Active account: true
      - Git operations protocol: https
      - Token: gho_************************************
      - Token scopes: 'gist', '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'
    

    With these settings, pulling with both users should work. Alas, it doesn't:

    > cd ~/Git/gh-priv/arthurhobspice/arthur-test 
    
    > git pull
    remote: Repository not found.
    fatal: repository 'https://github.com/arthurhobspice/arthur-test.git/' not found
    
    > cd ~/Git/gh-vf/Vodafone-DE-GHEC/cbauer-test
    
    > git pull
    Already up to date.
    

    The only working user is the one that has done gh auth login last. When I do gh auth login with the other user, I can pull from the other user's repo but not from the first one anymore. Here's what git config -l reports about the credential helpers:

    
    credential.helper=osxkeychain
    credential.https://github.com.helper=
    credential.https://github.com.helper=!/opt/homebrew/bin/gh auth git-credential
    credential.https://gist.github.com.helper=
    credential.https://gist.github.com.helper=!/opt/homebrew/bin/gh auth git-credential
    

    Does the osxkeychain helper interfere maybe?

  15. mosheduminer commented on Feb 13, 2024

    @mosheduminer

    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-git as suggested by @thomhurst, and that has solved the issue.

  16. arthurhobspice commented on Feb 14, 2024

    @arthurhobspice
    Author

    In my last comment, I stupidly missed the important point: running gh auth switch between pulling from different repositories. That's what this story is about, isn't it? Indeed, everything works fine after gh auth setup-git. I suspect that this section in my original .gitconfig

    [credential "https://github.com/"]
    	helper = !/opt/homebrew/bin/gh auth git-credential
    

    (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-git and sorry for the inconvenience, this issue can be closed.

  17. williammartin commented on Feb 14, 2024

    @williammartin
    Member

    No problem @arthurhobspice, I'm still a little suspicious why this wasn't handled automatically during your original gh auth login in this step:

    ? Authenticate Git with your GitHub credentials? Yes
    

    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.

  18. blindwiz commented on Feb 23, 2024

    @blindwiz

    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?

  19. williammartin commented on Feb 23, 2024

    @williammartin
    Member

    gh doesn't touch the user.name or user.email

    See #8590 (comment) for further discussion.

  20. RoyLeviGit commented on Mar 25, 2024

    @RoyLeviGit

    I would like to wake up this thread again as I agree as said above that gh auth login should do the setup done by gh auth setup-git that solves this issue.

    I believe otherwise this thread would keep coming up for people with the same issue just like it did for me :)

  21. williammartin commented on Mar 25, 2024

    @williammartin
    Member

    @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.

  22. MilanObrenovic commented on Jul 8, 2025

    @MilanObrenovic

    I've just run gh auth setup-git and that seems to have fixed my problem!

    this fixed it for me

  23. AnilGupte commented on Sep 4, 2025

    @AnilGupte

    gh auth setup-git

    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!

  24. williammartin commented on Sep 4, 2025

    @williammartin
    Member

    Yeh the lack of output is tracked by #11191, thanks for chiming in.

  25. chi11ax commented on Oct 29, 2025

    @chi11ax

    Had this issue today too. I didn't get to use gh auth setup-git as I logged out of both accounts to solve my problem before I found this. Do I need to run gh auth setup-git every time I switch or just once?

  26. williammartin commented on Oct 29, 2025

    @williammartin
    Member

    gh auth switch only needs to be run once, it configures gh as a credential helper in you ~/.gitconfig. What this means is that when git is working with a repo that has a remote on github.com (or whatever hosts you had logged in as when you run gh auth switch), it will ask gh for a token.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementgh-authmore-info-needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions