logoalt Hacker News

Kwpolskatoday at 9:33 AM8 repliesview on HN

Solving the problem of having a personal and a work GitHub account is really trivial without any extra tools. All you need is a dedicated SSH key for that GitHub account. (And why would you have a password for a ssh key on your personal machine?)

~/.ssh/config

    Host github.com-work
     HostName github.com
     User git
     IdentityFile ~/.ssh/work_id_rsa
     IdentitiesOnly yes
~/.git/config

    [user]
    email = [email protected]

    [remote "origin"]
    url = github.com-work:Work/Widget.git

Replies

embedding-shapetoday at 10:00 AM

Which works for a while, until you have a bunch of projects under various identities.

In my main ~/.gitconfig I have:

  [includeIf "gitdir:/home/user/projects/embedding-shapes/"]
  path = /home/user/.gitconfig-embedding-shapes
Where basically `projects/` follow GitHub naming with $user/$repo, so I set the git identity based on all projects within that user, rather than repo-by-repo which would get cumbersome fast.

Then you just make sure you're in the right directory :)

show 2 replies
hrpnktoday at 5:17 PM

The host shenanigans are simpler addressed with `git config core.sshCommand` which overrides the default key and selects the desired one on a per repository level.

source: https://erik.doernenburg.com/2017/12/using-multiple-github-a...

show 1 reply
arccytoday at 11:16 AM

If you don't want to bother with directories or want to use https instead of ssh, you can do remote url based dispatch in your gitconfig:

    [credential "https://github.com/org1"]
      useHttpPath = true
      helper =
      helper = /path/to/auth.sh user1
    [includeIf "hasconfig:remote.*.url:https//github.com/org1/**"]
      path = user1.gitconfig
      ; set name / email in user1.gitconfig
where auth.sh is something that can produce the right token for the given user, e.g.

    #!/bin/bash
    echo "username=$1"
    echo "password=$(gh auth token --user $1)"
xntoday at 11:51 AM

Are there any good reasons to use multiple GitHub user accounts? GitHub organization membership and permissions are well designed in my experience, negating the need for multiple user accounts.

show 4 replies
OrderlyTiamattoday at 1:35 PM

> (And why would you have a password for a ssh key on your personal machine?)

You're presumably joking? If not, could you elaborate?

show 1 reply
jimmydoetoday at 12:48 PM

For Claude Code users:

- using alternative host is not supported when roaming between local and cloud, fix is to add another origin you don’t use but use GitHub.com url

- CC uses gh command, which still needs account switch, this can be solved by add the switch to CC hook.

adithyassekhartoday at 1:00 PM

Is this that dropbox moment again? Anyway on Windows I keep a separate work and personal profile and GitHub auth doesn't go between them.