17 comments

[ 2.9 ms ] story [ 39.2 ms ] thread
For the use case of "use different accounts / configs for different directories", git's config has includeIf.
Lovley, was looking for exactly that for some time. Will definitely try it, thanks for sharing!
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
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.
Github account switching. A git account is not an idea that makes sense.
Yeah. Unfortunately, just goes to show how many people think Git = Github.
you can have per repo and per directory git config via git conditional includes

    [includeIf "hasconfig:remote.*.url:https://*.github.com/**"]
    path = /home/xani/src/gh/.gitconfig
Quite nice. I had to split into two statements to make it work for both https and ssh because of a bug in git:

    [includeIf "hasconfig:remote.*.url:git@git.work:*/**"]
      path = ~/.gitconfig.work
    [includeIf "hasconfig:remote.*.url:https://git.work/**"]
      path = ~/.gitconfig.work
Feel free to have your own workflow but git already addresses this tools entire purpose with includeIf.

what does this address that includeIf does not?

When did using a single per-machine SSH key on a Yubikey become so dirty in the eyes of these enterprises?
what does this do more than gh switch? I would rather something that automatically picks the right user based on the org
This is nice! This was a problem I faced a few years ago at my job. What I did was create a custom Bash function to switch Git accounts and add it to my .bashrc file. I love this. I’ll give it a try.
For the folks looking at tools to help manage personal and work identities on the same computer: don't.

Never access personal accounts from a work computer or work accounts from a personal computer under any circumstances.

This goes for laptops, desktops, and especially cellphones.

If an employer asks that you violate the above, ask for a dedicated device owned by your employer to access a work account. If they refuse, that's a big red flag. "Oh just use your phone to check your email/slack" - 1. don't assume everyone has a cellphone and 2. if you want folks doing work on a device, pay for it.

Managing multiple personal accounts on computer A or multiple work accounts on computer B is totally fine.

As an aside, company general counsels might be shocked at how often their employees log in to slack/email/etc. from their personal cellphone: suddenly any and all company and customer intellectual property has a way to leave the network. And it's not even a "pull" from the employee as the other employees just "push" them messages.