Remix.run Logo
Gitas – A tool for Git account switching(github.com)
18 points by letmutex 4 days ago | 14 comments
Kwpolska 3 hours ago | parent | next [-]

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 = work@example.com

    [remote "origin"]
    url = github.com-work:Work/Widget.git
embedding-shape 3 hours ago | parent | next [-]

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 :)

beaker52 17 minutes ago | parent | next [-]

This. I’ve seen so many tools solving problems that already have solutions lately because LLMs allow people to run off and “fix” the problem their way before they can a chance to discover existing, more appropriate solutions.

The next step of this problem space is: “when I’m working on project X, I often forget to change my GitHub user with Gitas” so now they need direnv or something to switch it for them. The original solution foresaw this - so is far more complete that Gitas already _and_ built into git itself.

But, LLMs, so here we are, slowly drowning in a growing ocean of software built by the unaware.

accoil an hour ago | parent | prev [-]

I use that approach. I also make sure to not set the [user] section in my main config (and only in the included files). That way if I'm operating outside of one of my user directories git commit fails due to having no user details.

arccy 2 hours ago | parent | prev | next [-]

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)"
xn an hour ago | parent | prev [-]

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.

embedding-shape an hour ago | parent [-]

> Are there any good reasons to use multiple GitHub user accounts?

Is there any good reasons not to separate what you work on into multiple GitHub accounts? Not to mention some people don't want all their projects attached to one profile, some people also develop in their free-time, and don't want to mix freetime/work projects under the same user account, for multiple reasons.

eqvinox an hour ago | parent | prev | next [-]

You can put [user] blocks in repos, i.e.

  /some/where $ head .git/config
  [user]
   email = me@example.org
   name = My 'nick' Name
Doesn't tie into your SSH key though, if you need that.
turbocon 37 minutes ago | parent [-]

You can manage multiple ssh keys via your ssh config too. But this does seem to make things easier, I always end up fighting with this when I need to do it once every 3 years

sevensor an hour ago | parent | prev | next [-]

Github account switching. A git account is not an idea that makes sense.

embedding-shape 42 minutes ago | parent [-]

It's actually about git account switching as far as I can tell, which does make sense, you can have multiple "git" users indeed. Maybe it's the wording that is wrong? Read "account" as "user" and it might make more sense :)

  # in ~/.gitconfig
  [includeIf "gitdir:/home/user/projects/embedding-shapes/"]
    path = /home/user/.gitconfig-embedding-shapes

  # in ~/.gitconfig-embedding-shapes
  [user]
  name = embedding-shapes
  email = embedding-shapes@proton.me

  [core]
  sshCommand = ssh -i /home/user/.ssh/id_embedding-shapes
That's one of my git "accounts", currently I have four in total, one being my "real identity", other are pseudo-anonymous users.
dvratil 2 hours ago | parent | prev | next [-]

I used to have a git post-checkout hook that set the repo identity based on the repo origin url [0] on checkout - maybe there's some post-clone hook these days, but 10 years ago when I wrote it there was only post-checkout hook.

[0] https://www.dvratil.cz/2015/12/git-trick-%23628-automaticall...

rgoulter 4 hours ago | parent | prev | next [-]

For the use case of "use different accounts / configs for different directories", git's config has includeIf.

7777777phil 3 hours ago | parent | prev [-]

Lovley, was looking for exactly that for some time. Will definitely try it, thanks for sharing!