Skip to content
barnuriPublic

About

Use the right GitHub account in every folder: a thin per-directory profile wrapper for the gh CLI

Resources

Stars

8 stars

Watchers

1 watching

Forks

Repository files navigation

ghx

Use the right GitHub account in every folder. ghx is a thin wrapper around the official gh CLI. It picks a gh account from the directory you are in, then runs the real gh with your arguments unchanged.

ghx demo

  • Every gh command, flag and extension works, because ghx passes them through as-is.
  • gh keeps its own updates. ghx never patches or bundles it.
  • macOS, Linux and Windows. One static binary, no runtime.
  • Installs as gh, so scripts, editors and aliases use it with no changes.
  • Updates itself in the background, once a day.

Install

macOS / Linux:

curl -fsSL https://raw.githubusercontent.com/barnuri/ghx/master/install.sh | sh

Windows (PowerShell):

irm https://raw.githubusercontent.com/barnuri/ghx/master/install.ps1 | iex

The installer downloads the latest release for your OS and CPU, checks its sha256, and installs ghx plus a gh shim that points to it:

OS Install dir gh shim
macOS / Linux ~/.local/bin symlink gh -> ghx
Windows %LOCALAPPDATA%\Programs\ghx copy gh.exe

Put the install dir before the real gh in PATH. ghx skips its own dir when it looks for the real gh, so the shim never calls itself. Set GHX_SHIM=0 to install only ghx, or GHX_INSTALL_DIR to pick another dir. On Windows, run the installer as Administrator when gh is in the system PATH, because Windows searches the system PATH before the user PATH.

With Go: go install github.com/barnuri/ghx@latest, then alias gh=ghx.

Configuration

  1. Log in to each account once. gh stores all of them:

    gh auth login   # work account
    gh auth login   # personal account
  2. Map folders to accounts in ~/.config/ghx/config.yaml (Windows: %APPDATA%\ghx\config.yaml, or set GHX_CONFIG):

    profiles:
      work:
        user: my-work-login
      personal:
        user: my-personal-login
    
    dirs:
      ~/code/work: work
      ~/code/personal: personal

Now every folder under ~/code/work, at any depth, uses the work account, and every folder under ~/code/personal uses the personal account:

cd ~/code/work/api/src && gh pr list        # as my-work-login
cd ~/code/personal/blog && gh repo view     # as my-personal-login

Full example: examples/config.yaml.

Key Meaning
profiles.<name>.user gh login, as shown by gh auth status
profiles.<name>.host GitHub Enterprise host. Default: github.com
profiles.<name>.name / .email git user.name / user.email for repos of this profile. See below
profiles.<name>.git_identity once or always. See below
profiles.<name>.identity_from_github true takes a missing name or email from the GitHub account. See below
name / email / git_identity / identity_from_github Fallback for each profile that does not set the key
dirs folder -> profile. The deepest matching folder wins
ssh_hosts ssh host alias in the origin remote URL -> profile. See below
orgs GitHub org of the origin remote -> profile. * and ? wildcards work, e.g. "chkp-*": work. An exact name beats a wildcard
remote_user true uses the username in an https origin remote URL. See below
default profile for folders outside every dirs entry. Default: plain gh
manage_gh true makes ghx download gh and keep it updated. Default: use gh from PATH
disable_updates true stops the daily ghx update check

Per-repo file: .ghx

Put a .ghx file in a repo to choose its profile. It holds one line: a profile name, or a gh login if no profile has that name.

echo personal > .ghx
echo .ghx >> .git/info/exclude   # or add it to .gitignore

ghx uses the nearest .ghx in the current folder or any parent folder.

SSH host aliases: ssh_hosts

If you keep one ssh key per account with host aliases in ~/.ssh/config:

Host gh-work
    HostName github.com
    IdentityFile ~/.ssh/work

and clone with git clone git@gh-work:org/repo.git, map the alias to a profile:

ssh_hosts:
  gh-work: work
  gh-personal: personal

ghx reads the host from git remote get-url origin (scp-like git@gh-work:org/repo or ssh://git@gh-work/org/repo). It runs git only when ssh_hosts or remote_user is set.

HTTPS remote username: remote_user

If you put your login in the https remote URL (it also works well with Git Credential Manager):

git clone https://[email protected]/org/repo.git

turn on remote_user:

remote_user: true

ghx uses the profile whose user and host match the URL. If no profile matches, it uses the username as the gh login, on the URL's host.

Git identity: name and email

ghx can set the repo-local git user.name and user.email from the matched profile. It is off until you set name or email:

name: My Name                # fallback for every profile
email: [email protected]
profiles:
  work:
    user: my-work-login
    email: [email protected]       # name comes from the fallback
    git_identity: always

Each time ghx runs in a git repo with a matched profile, it applies git_identity:

  • once (default): sets a key only when the repo has no local value for it. A value you set by hand stays.
  • always: replaces a local value that is different.

With no matched profile, ghx does not change git config.

Set identity_from_github: true to take a name or email that neither the profile nor the fallback sets from the profile's GitHub account. The name is the account's display name. The email is the first of:

  1. The primary email. This needs the user token scope: gh auth refresh -s user --user <login>.
  2. The public profile email.
  3. On github.com, the noreply email <id>+<login>@users.noreply.github.com.

ghx calls GitHub only when it is about to write a key, and caches the result for each login for one day.

Resolution order

  1. GHX_PROFILE environment variable
  2. Nearest .ghx file
  3. ssh_hosts entry for the origin remote's host
  4. Username in the origin remote URL, when remote_user is true
  5. Org of the origin remote, when orgs matches it
  6. Deepest matching dirs entry
  7. default
  8. No match: plain gh, with your active gh account

Commands

ghx adds one command group, gh ghx. Everything else goes to gh.

gh ghx which     Show the profile, gh user and gh binary for the current folder
gh ghx current   Print only the profile name (empty when none), for prompts and status lines
gh ghx update    Update ghx now (and gh when manage_gh is true)
gh ghx version   Show the ghx version

How it works

ghx runs gh auth token --user <login> and starts the real gh with GH_TOKEN set (GH_ENTERPRISE_TOKEN and GH_HOST for an Enterprise host). It never runs gh auth switch, so terminals in different folders do not change each other's account.

  • gh auth ... commands run without the token, so gh auth login and gh auth status work as usual. gh auth git-credential gets the token, so git pushes as the folder's account.
  • A GH_TOKEN you set yourself wins over ghx.
  • Exit codes, stdin, stdout and the TTY belong to gh. On macOS and Linux ghx replaces itself with gh (exec).

To make git push use the folder's account, point git at the shim:

git config --global credential.https://github.com.helper ''
git config --global --add credential.https://github.com.helper "!$HOME/.local/bin/ghx auth git-credential"

Use the full ghx path. VS Code and other apps started from the macOS Dock get a short PATH without ~/.local/bin, so a bare gh fails with gh: command not found. ghx also checks /opt/homebrew/bin, /usr/local/bin and /home/linuxbrew/.linuxbrew/bin for the real gh when PATH does not have it.

Updates

Once a day ghx starts a background process that downloads the latest ghx release, checks its sha256 and replaces the binary. With manage_gh: true it does the same for gh from cli/cli releases. The result goes to update.log in the ghx cache dir (~/Library/Caches/ghx, ~/.cache/ghx or %LOCALAPPDATA%\ghx). Set GHX_NO_UPDATE=1 or disable_updates: true to turn it off.

Development

go test ./...
vhs demo/demo.tape   # re-record docs/demo.gif with a stub gh and demo accounts

Every push to master runs the tests on macOS, Linux and Windows, then publishes release v0.1.<run> with binaries for darwin, linux and windows on amd64 and arm64.

License

MIT

About

Use the right GitHub account in every folder: a thin per-directory profile wrapper for the gh CLI

Resources

Stars

8 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages