Skip to content

Instantly share code, notes, and snippets.

@yermulnik
Last active April 14, 2025 02:05
Show Gist options
  • Select an option

  • Save yermulnik/017837c01879ed3c7489cc7cf749ae47 to your computer and use it in GitHub Desktop.

Select an option

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

yermulnik commented Apr 11, 2022

Copy link
Copy Markdown
Author

Tip

💡 As of https://github.com/cli/cli/releases/tag/v2.40.0 (20231207) GH CLI provides this feature natively:

With the v2.40.0 release, multi-account support is introduced to gh for GitHub.com and GitHub Enterprise

Example hosts.yml.personal content:

> cat ~/.config/gh/hosts.yml.personal
github.com:
    oauth_token: ghp_[…]
    git_protocol: ssh
    user: yermulnik

Example ~/.config/gh/hosts.yml.work content:

> cat ~/.config/gh/hosts.yml.work
github.com:
    oauth_token: ghp_[…]
    git_protocol: ssh
    user: george_yermulnik

Add GH CLI aliases programmatically (or manually copy contents of config.yaml to ~/.config/gh/config.yaml):

> gh alias set --shell personal 'cp ~/.config/gh/hosts.yml.personal ~/.config/gh/hosts.yml && gh auth status'
> gh alias set --shell work 'cp ~/.config/gh/hosts.yml.work ~/.config/gh/hosts.yml && gh auth status'

Example usage:

> gh personal
github.com
  ✓ Logged in to github.com as yermulnik (/home/yermulnik/.config/gh/hosts.yml)
  ✓ Git operations for github.com configured to use ssh protocol.
  ✓ Token: *******************

Use gh work to switch to your work profile.

Note

🪟 Windows users: replace ~/.config/gh with C:\Users\YOUR_USERNAME\AppData\Roaming\GitHub CLI (config.yaml goes to the same location)

Note

In some conditions it might also be necessary to run gh auth setup-git after copying the file(s) (see this post down the thread):

aliases:
    personal: '!cp ~/.config/gh/hosts.yml.personal ~/.config/gh/hosts.yml && gh auth setup-git && gh auth status'
    work: '!cp ~/.config/gh/hosts.yml.work ~/.config/gh/hosts.yml && gh auth setup-git && gh auth status'

Note

UPD (20221012): gabe565/gh-profile provides similar functionality by means of GH CLI extension

@neeraj-htp

Copy link
Copy Markdown

where do I put config.yml?

@yermulnik

Copy link
Copy Markdown
Author

@neeraj-htp Into ~/.config/gh/

@josephwibowo

Copy link
Copy Markdown

thank you for the workaround...2 years on that issue and no update

@AcrylicShrimp

Copy link
Copy Markdown

God, thank you

@imjlk

imjlk commented Jun 23, 2022

Copy link
Copy Markdown

Thank you
In Windows, config.yml exist in C:\Users\your-username\AppData\Roaming\GitHub CLI

@kmarinoff

Copy link
Copy Markdown

Thank you!

@Thaonhi1998

Thaonhi1998 commented Jul 14, 2022

Copy link
Copy Markdown

#config.yml

@timrodz

timrodz commented Jul 19, 2022

Copy link
Copy Markdown

tyvm!

@hnpn11

hnpn11 commented Jul 19, 2022

Copy link
Copy Markdown

Thank you! Save a lot of time to re-authenticate

@rizperdana

Copy link
Copy Markdown

doing the lord work, thank you sir

@raul-floatme

Copy link
Copy Markdown

This is really dang helpful.

Thanks a ton!

@DaphyMorph

Copy link
Copy Markdown

thank you very much!

@tysonxor

Copy link
Copy Markdown

f*cking love you

@devhell

devhell commented Sep 7, 2022

Copy link
Copy Markdown

This is the best thing since sliced cheese. I do hope this will be solved by upstream. Thank you!

@farmeroy

Copy link
Copy Markdown

So good!

@jlanz

jlanz commented Mar 13, 2023

Copy link
Copy Markdown

Thanks!

@harryduong91

Copy link
Copy Markdown

This is true the love of my life

@farhan-helmy

Copy link
Copy Markdown

thank you T_T

@MammadovAdil

Copy link
Copy Markdown

Thank you very much. It works great. But I had to add gh auth setup-git to the aliases to make git recognize switched profiles. So, my aliases inside ~/.config/gh/config.yml looks like as below currently:

aliases:
    co: pr checkout
    personal: '!cp ~/.config/gh/hosts.personal.yml ~/.config/gh/hosts.yml && gh auth setup-git && gh auth status'
    work: '!cp ~/.config/gh/hosts.work.yml ~/.config/gh/hosts.yml && gh auth setup-git && gh auth status'

@alpineyahoo

Copy link
Copy Markdown

This method works like a charm with direnv.
Suppose you have

~/work
~/personal

Put .envrc in each dir and write the same thing.
ex.) ~/work/.envrc

/bin/cp ~/.config/gh/work.yml ~/.config/gh/hosts.yml && gh auth status

When you cd ~/work, the command above is automatically run by direnv.

@yermulnik

Copy link
Copy Markdown
Author

https://github.com/cli/cli/releases/tag/v2.40.0

With the v2.40.0 release, multi-account support is introduced to gh for GitHub.com and GitHub Enterprise and it is only the beginning!

@IAmIlliest

IAmIlliest commented Apr 13, 2025

Copy link
Copy Markdown

Thanks. The one thing that is missing for me (...or I just haven't worked it out yet):

Can you use a hostname other than 'github.com'?
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?

Still love this overall though, thanks!

@yermulnik

Copy link
Copy Markdown
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
Copy Markdown

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