Skip to content

Instantly share code, notes, and snippets.

@yermulnik
Last active May 3, 2024 21:08
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'
@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