DEV Community

Cover image for 1Password, GitHub, and Git CLI with Multiple Users
Sean Boult
Sean Boult

Posted on

1Password, GitHub, and Git CLI with Multiple Users

Do you use the git, github, and 1password CLI? I do!

I want to be able to act as different users in certain directories to support my personal projects (~/code) and my work projects (~/oss) automatically, because, you know, developers automate everything.

This means I want to be able to run gh commands as the correct user, as well as have git commands pick the correct SSH key.

If you want to have a single dotfiles repo for both work and personal development, this should give you something to follow.

⚠️ If you use a single GitHub account for everything, this article might not be very helpful for you.

Prerequisites

Tools you’ll need to make all this work:

You’ll also want to designate some folders to hold projects. This guide will use the following structure:

  • ~/code - where I do personal development on my personal machine
  • ~/oss - where I do all my work open source code (repros, demos, etc.)

1) Set a global default (personal)

In ~/.gitconfig:

[user]
    name = Sean Boult
    email = 996134+Hacksore@users.noreply.github.com

[includeIf "gitdir:~/oss/"]
    path = ~/.gitconfig-work
Enter fullscreen mode Exit fullscreen mode

So we’ve added the default user in the [user] block and added an includeIf (docs) block that specifies that inside ~/oss we’ll make sure to load the config options in ~/.gitconfig-work.

Given that you need to be “inside a git directory” based on the gitdir clause, it means that ~/oss should be a valid Git directory.

To do that, run this in ~/oss:

# make ~/oss a valid git repo
git init
Enter fullscreen mode Exit fullscreen mode

We will also want to create a .gitignore file to not track anything in this directory.

# ~/oss/.gitignore
# Sentinel repo: exists only so includeIf matches when cwd is ~/oss
# Ignore all subdirs (they're separate repos)
*
Enter fullscreen mode Exit fullscreen mode

2) Put work identity in a separate config

Create ~/.gitconfig-work:

[user]
    name = Sean Boult
    email = <yourWorkEmail> # replace this with your email
    # optional ssh key signing
    # signingkey = ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

3) Make SSH key selection automatic

Now configure SSH so Git will pick the right key based on the repo you are in.

In ~/.ssh/config:

# Github and we default personal
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa.personal
    IdentitiesOnly yes
Enter fullscreen mode Exit fullscreen mode

Then, in ~/.gitconfig-work, add:

[core]
    sshCommand = ssh -i ~/.ssh/id_rsa -o IdentitiesOnly=yes
Enter fullscreen mode Exit fullscreen mode

At this point:

  • Any repo elsewhere uses your personal Git identity and personal SSH key.
  • Any repo under ~/oss uses your work Git identity and work SSH key.

1Password CLI Plugin

The next step is to allow the gh CLI to know what directory you’re in and use the right GitHub PAT (personal access token).

To put it simply, I have two PATs in my 1Password vault: gh-pat-cli (personal) and gh-pat-cli-sboult (work/oss). In the op setup, instruct 1Password to use the gh-pat-cli-sboult PAT so that when you’re in ~/oss, it uses the correct PAT to authenticate with the GitHub CLI.

Use 1Password to securely authenticate the GitHub CLI is a helpful doc if you get stuck.

Work repos (~/oss)

Example:

cd ~/oss

git clone git@github.com:awslabs/mcp.git
# or
# gh repo clone awslabs/mcp
Enter fullscreen mode Exit fullscreen mode

Once the repo is under ~/oss/, Git uses:

  • your work user.name / user.email
  • your work SSH key

Then normal commands work:

git remote -v
git push

gh pr list
Enter fullscreen mode Exit fullscreen mode

Personal repos

Outside of ~/oss, everything uses your default (personal) identity.

Example:

git clone git@github.com:Hacksore/test.git
# or
# gh repo clone test
Enter fullscreen mode Exit fullscreen mode

That’s it! You should now be able to reproduce the setup I have.

Happy coding 😊!

Top comments (0)