Skip to content

Instantly share code, notes, and snippets.

@yermulnik
Last active May 3, 2024 21:08
Show Gist options
  • Star 81 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • 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

yermulnik commented Apr 11, 2022

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 config.yaml to ~/.config/gh/):

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

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

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

@neeraj-htp
Copy link

where do I put config.yml?

@yermulnik
Copy link
Author

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

@josephwibowo
Copy link

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

@AcrylicShrimp
Copy link

God, thank you

@imjlk
Copy link

imjlk commented Jun 23, 2022

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

@kmarinoff
Copy link

Thank you!

@Thaonhi1998
Copy link

Thaonhi1998 commented Jul 14, 2022

#config.yml

@timrodz
Copy link

timrodz commented Jul 19, 2022

tyvm!

@hnpn11
Copy link

hnpn11 commented Jul 19, 2022

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

@rizperdana
Copy link

doing the lord work, thank you sir

@raul-floatme
Copy link

This is really dang helpful.

Thanks a ton!

@DaphyMorph
Copy link

thank you very much!

@adamtysonsmith
Copy link

f*cking love you

@devhell
Copy link

devhell commented Sep 7, 2022

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

@farmeroy
Copy link

So good!

@jlanz
Copy link

jlanz commented Mar 13, 2023

Thanks!

@harryduong91
Copy link

This is true the love of my life

@farhan-helmy
Copy link

thank you T_T

@MammadovAdil
Copy link

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

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
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!

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