Repository navigation
Document the relationship between hosts and active accounts #9021
Description
Activity
Hey @danfbfern, sorry you're running into issues. I think there's a bit of confusion here and as such I'm not really sure what to make of your bug report.
The CLI and Hosts
The CLI only targets one host at a time for any invocation. If you are authenticated with two hosts, it will default to targeting
github.com. If you are authenticated with only one host, it will target that host. If you are authenticated with zero hosts, it will targetgithub.com. You can change the targeted host by setting theGH_HOSTenvironment variable (more details can be found ingh environment).Therefore, it is correct that you have 2 active accounts (1 per host). If you had 2 accounts on a single host then one would be active and one would be inactive.
So what are you seeing?
You said:
I'm able to push, commit, etc with both my accounts but apparently I have been committing to the enterprise with my personal one because both of the accounts are shown as active, and the gh auth switch doesn't do anything.
Let's take
ghout of the picture altogether for a second. If your accounts live on different hosts, you cannot commit to one as the other (I assume bycommityou meanpush a git commit). Can you provide some more information about what you're actually seeing, preferably with some output copied from your terminal?Cheers.
- addedmore-info-neededMore info needed from user/contributorMore info needed from user/contributorand removedneeds-triageneeds to be reviewedneeds to be reviewed
on Apr 29, 2024 Hey @williammartin
Regarding the first part related to hosts, if both arre active because one is enterprise and the other is from github.com, then how do I know im using the right account to commit and push code (both things related to my company and personal stuff)?
About your question, in a company repo (from github enterprise) I have access to it from my enterprise credentials but whenever I'm commiting and pushing code, its showing up with my personal user instead.

Shouldn't I be able to choose my account and if the active one was the personal then I wouldn't be able to commit to it?
Regarding the first part related to hosts, if both arre active because one is enterprise and the other is from github.com, then how do I know im using the right account to commit and push code (both things related to my company and personal stuff)?
There's a few answers to this but remember at it's core that credentials for an account on one host cannot be used to authenticate with another host.
but whenever I'm commiting and pushing code, its showing up with my personal user instead.
Now I think I understand what you mean! The
git login your screenshot shows the git commit objects which has metadata about the git commit including youruser.nameanduser.email. When yougit commit,gitpulls information from your git config to fill in the commit details. You can look into your git config with information from here. These details are totally unrelated to your GitHub accounts, you could put anything in here you like:➜ git config user.name "Mona Lisa" ➜ git config user.email "[email protected]" ➜ git commit --allow-empty [main f626508] Mona ➜ test-repo git:(main) ✗ git log | cat commit f626508e6800955293ad90a236c88bcfd6d90e24 Author: Mona Lisa <[email protected]> Date: Mon Apr 29 19:36:55 2024 +0200 MonaAnd
ghdoesn't currently try to do anything with youruser.nameanduser.email.You may find some ways to use
IncludeIfto change yourgit configdepending on the targeted remote in #326 but that's totally outside the realm ofghright now.To add to @williammartin's explanation, I think you can modify your
~/.gitconfigfile to apply differentuser.nameanduser.emailbased on the directory where your clone resides. For example if you have something like this in your.gitconfigfile:[user] name = Mona Lisa email = [email protected] [includeIf "gitdir:~/company/"] name = Employee 47 email = [email protected]
This instructs
git(notghCLI), to use a different name/email when the repo is under~/companydirectory.Reacted by William Martin and dav3yblaz3Ohhh thats unfortunate, I though that
ghwould handle already with the recently addedgh auth switch.From the documentation its not really clear that
gh auth switchwould have multiple accounts as active from different hosts, especially given the examples.
https://cli.github.com/manual/gh_auth_switchIt looks like it possible to change from an account on a host to another account on a totally different host, meaning that only one of them would be active.
The solution to use the
includeIfis kinda of a workaround because that forces me to separate work directories with personal directories instead of separating by type of repo which is something I'm used to it, making a need to put everything inside that directory, but its acceptable for nowSince I wrote the documentation for and implemented
auth switch, it's hard for me to see it through any other lens so I'd greatly appreciate if you had some suggestions that would have made the documentation clearer for you!Sure thing, that makes sense.
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.
Using this quote, it leads to believe that there will be only one active account but it does mention on a specific host, which explains with I have one active in each host.
But the follow up examples (the last one especially) may lead me back to thinking that I could switch from an account on a specific host to another account on another host, making that one active.
# Switch to a specific host and specific account $ gh auth switch --hostname enterprise.internal --user monalisaIf we reworded:
Switch to a specific host and specific account
To
Switch the active account on a specific host to a specific user
Would that have been clearer?
Yes that would help, and I would personally add a line before the examples and maybe in the gh auth status doc also, to clarify that each host will always have one active account.
Something like:
For environments with multiple hosts, the
gh authsupports having one active account per host simultaneously. Thus, if you authenticate with different accounts on separate hosts, each account will remain active.@danfbfern If you don't have personal and work-related repos in separate directories, I think you can use a different condition like this in your
.gitconfigfile:[includeIf "hasconfig:remote.*.url:**/company/**"] name = Employee 47 email = [email protected]
Or if that didn't work, this:
[includeIf "hasconfig:remote.*.url:https://github.com/company/**"] name = Employee 47 email = [email protected]
See here for more about the
hasconfig:remote.*.urlcondition.Thanks @babakks
Didn't knew about that option, I'll try that because it seems like a better approach for my caseReacted by Babak K. Shandiz- changed the title
[-]All accounts are active[/-][+]Document the relationship between hosts and active accounts[/+]on May 1, 2024 - addedgh-authrelating to the gh auth commandrelating to the gh auth commandcoreThis issue is not accepting PRs from outside contributorsThis issue is not accepting PRs from outside contributorsand removedbugSomething isn't workingSomething isn't workingmore-info-neededMore info needed from user/contributorMore info needed from user/contributor
on May 1, 2024 - linked a pull request that will close this issueDocument relationship between host and active account #9032
on May 1, 2024 Having added documentation in #9032 to address the confusion, you having a workaround, and there already being work tracking the
user.nameanduser.emailfeatures, I'm closing this. Thanks everyone for your involvement.
Describe the bug
Have two different accounts, one is my personal one and the other its from from github enterprise.
I'm able to push, commit, etc with both my accounts but apparently I have been committing to the enterprise with my personal one because both of the accounts are shown as active, and the
gh auth switchdoesn't do anything.For context, I have two different SSH keys and used each one to configure the gh cli but was expecting to have one as active and the other to not be active.
Tried doing it all over again, with HTTPS instead with no luck
I'm using gh version 2.48.0
Steps to reproduce the behavior
gh auth statusto see if both are active at the same timeExpected vs actual behavior
I expected to one account with active on
trueand the other onfalse