DEV Community

Guilherme Samuel
Guilherme Samuel

Posted on • Edited on

2

How to use the GH CLI with 2 accounts

This article solves the following questions:
How to use gh with 2 accounts, how can I use gh with 2 github accounts, multiple gh-cli accounts on one machine, github cli multiple accounts


It's incredibly boring having to do a gh auth login every time you want to use a different account. Today we're going to learn how to solve that by using an extension which allows us to create multiple profiles and switch between them based on the current directory. To do so, the steps are really simple:

  1. Install the extension
    gh extension install gabe565/gh-profile

  2. Create your first profile
    gh profile create [NAME]

  3. Login as you usually do
    gh auth login

Now, you have created a profile with an associated github account. You can create other ones too by following the steps 2 and 3 above.

If you want to switch between multiple profiles you can use gh profile switch [NAME]

That's better, but I promised you we were going to be able to use different profiles based on the current directory. In order to achieve it, follow these steps:

  1. Install and setup direnv following their official tutorial: https://direnv.net/docs/installation.html

  2. Whenever you want to set a profile for the current repo, use the -l option:
    gh profile switch [NAME] -l

Done. Now whenever you use your terminal on the current directory, is going to be using that specific github profile. No need to keep switching anymore!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (1)

Collapse
 
owlrazum profile image
Owlrazum

Thanks for the info, registered here specifically to thank you!

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay