Skip to content

Instantly share code, notes, and snippets.

@yermulnik
Last active April 14, 2025 02:05
Show Gist options
  • Save yermulnik/017837c01879ed3c7489cc7cf749ae47 to your computer and use it in GitHub Desktop.
Save yermulnik/017837c01879ed3c7489cc7cf749ae47 to your computer and use it in GitHub Desktop.
GH CLI multi-account switch
git_protocol: ssh
aliases:
personal: '!cp ~/.config/gh/hosts.yml.personal ~/.config/gh/hosts.yml && gh auth status'
work: '!cp ~/.config/gh/hosts.yml.work ~/.config/gh/hosts.yml && gh auth status'
@yermulnik
Copy link
Author

Can you use a hostname other than 'github.com'?

You should be able to. Try replacing the github.com in hosts.yml.<whetever_you_named_it> with the actual GHE hostname that you use to connect to your GHE instance.

For instance if your .ssh/config has a 'github-work' and a 'github-personal' how would one point the alias (or hosts.yml.work, not sure where it would need to happen) to use the 'github-work' hostname from .ssh/config?

Don't think it's feasible as GH CLI and SSH CLI don't have anything in common around SSH host aliases.
To the best of my knowledge, in GH CLI config you have to use actual GH instance hostname and it doesn't support aliasing. But you should be good to go with actual GHE instance hostname.

Still love this overall though, thanks!

Nowadays you definitely should better use gh auth login and gh auth switch (see gh auth login --help and gh auth switch --help for details on args).

@IAmIlliest
Copy link

Thanks for the quick response, and I'll look into gh switch and multiple accounts too. Cheers.

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