Switch between GitHub accounts using the gh CLI and Bash

I have different accounts on GitHub that I use for different projects. Here are two tips on how to switch between your accounts depending on which project you’re working on.

It’s really handy to be able to push code with the right Git user without having to log in and out all the time. GitHub CLI has a command called gh auth switch which seems to sort everything out, but I noticed that my commits weren’t being made using the account I’d already switched to. I had to restart my Kitty terminal for that change to take effect. But it shouldn’t have to be like that. When I’m working in the terminal, I’d rather not have to leave it and the task I’m working on just because something needs to reload.

What does the gh auth switch actually do?

gh auth switch changes which GitHub user is active in the GitHub CLI tool. This affects:

  • Which account gh commands use (e.g. gh pr create, gh issue list, gh repo clone)
  • Which authentication method Git uses when you run git push or git pull over HTTPS – if you have run gh auth setup once

This does not affect:

  • The Git configuration for my commits, i.e. user.name and user.email in this case.

There is a very simple solution to this, which is to specify which GitHub user should be associated with which project – in my case, which folder. I have two separate folders that divide my projects into personal and work projects. Here’s how to do it:

1# Go to your private project
2git config --local user.name "Lars Jönsson"
3git config --local user.email "crymeat@gmail.com"
4
5# and the same applies to your work project
6git config --local user.name "Lars Jönsson"
7git config --local user.email "info@noveris.se"

It wasn’t that difficult.

But there’s another solution I’ve decided to go with. Mainly because I like tinkering with Bash and the CLI in general. I’ve created a Bash script that changes everything at once.

1#!/bin/bash
2
3case "$1" in
4 "personal")
5 git config --global user.name "Lars Jönsson"
6 git config --global user.email "crymeat@gmail.com"
7 echo "Switching to devpaps git account..."
8
9 # Make devpaps active GitHub CLI account
10 gh auth status | grep "devpaps" > /dev/null
11 if [ $? -eq 0 ]; then
12 gh auth switch -h github.com -u devpaps
13 else
14 echo "devpaps not logged in via gh. Logging in..."
15 gh auth login --hostname github.com
16 fi
17 ;;
18 "work")
19 git config --global user.name "Lars Jönsson"
20 git config --global user.email "info@noveris.se"
21 echo "Switching to Noveris git account..."
22
23 # Make noveris-hq active GitHub CLI account
24 gh auth status | grep "noveris-hq" > /dev/null
25 if [ $? -eq 0 ]; then
26 gh auth switch -h github.com -u noveris-hq
27 else
28 echo "noveris-hq not logged in via gh. Logging in..."
29 gh auth login --hostname github.com
30 fi
31 ;;
32 *)
33 echo "Usage: $0 {personal|work}"
34 exit 1
35 ;;
36esac
37
38# Show current Git user configuration
39echo -e "\nCurrent Git configuration:"
40echo "Name: $(git config --global user.name)"
41echo "Email: $(git config --global user.email)"
42
43echo "Reloading shell to apply GitHub credentials..."
44exec $SHELL

What the script does is first set my name and email as my Git username, then it checks whether noveris-hq is authenticated with gh-cli; if so, my username in gh-cli is also changed to noveris-hq; if not, I have to log in via gh-cli.

To use this Bash script, all I need to do is type:

1noveris on main +95 -93 [$!?] via v22.20.0 via 🐘 v8.5.4 ❯ switchgh work
2Switching to Noveris git account...
3 Switched active account for github.com to noveris-hq
4
5Current Git configuration:
6Name: Lars Jönsson
7Email: info@noveris.se
8Reloading shell to apply GitHub credentials...

Everything is handled automatically for me. I think this is really good. Plus, you feel a bit like a hacker when you get to type commands into the terminal, which is a bonus.

It might be a bit of overkill with my Bash script, but what does it matter in a hundred years time?

Mastodon