1. 39
  1. 28

    the same goal can usually be achieved with this one time setup (instead of per repo):

    multiple @github accounts without messing with hostnames in ssh configs & standard git: urls for cloning:

    ~/.gitconfig:
    [includeIf "gitdir:~/work/"]
    path = .gitconfig_work
    
    ~/.gitconfig_work:
    [user]
    email = …
    [core]
    sshCommand = ssh -i ~/.ssh/id_…_work -o 'IdentitiesOnly yes'
    

    https://twitter.com/wbolster/status/1127265684319293440

    1. 2

      This works great, thanks. Note: if you forget the trailing slash of the gitdir path, you get a standard permission denied error with AFAICT no options to debug since ssh connects fine, it’s just the filter that doesn’t work. So I thought I’d leave this here for future reference.

    2. 3

      A problem arises with this trick when your repo uses submodules that can only be accessed by a specific account. The problem is that you’ll need to change the urls in your .gitmodules file as well, which you then can’t commit. If anyone happens to know a way around that, I’d love to know :)

      1. 3

        see my other comment or this tweet

        https://twitter.com/wbolster/status/1127265684319293440

      2. 3

        Well, the trick is not a trick at all - this is pretty standard OpenSSH config. Obviously, not limited to Git or GitHub, et al.

        In terms of actual Git, I like @wbolster’s solution much more, especially if one copy-pastes URLs a lot - no need to remember to modify them.

        1. 3

          Please note that this may technically violate GitHub’s terms of service, see section B.3. Account Requirements:

          • One person or legal entity may maintain no more than one free Account (if you choose to control a machine account as well, that’s fine, but it can only be used for running a machine).
          1. 7

            Not necessarily - the clue is in the word free. One can have a free personal account and a business work account, and all is well :^)

            1. 1

              Thanks for pointing this out. I’ve adjusted my wording to make clear you’re not always going to be in violation.

          2. 3

            Separate local users can do the job, too. Each gets its own ~/.gitconfig and ~/.ssh.

            1. 1

              That’s a neat trick.

              However, I wonder what kind of motivation people have for leading a double life that don’t require stronger anonymity measures than just separate accounts.

              1. 2

                I’d sometimes do personal stuff on my work laptop at home at my last job. This would have been great to have then.

                1. 1

                  If you do it sometimes, then pushing over HTTPS with user/password auth doesn’t seem like a serious inconvenience. It also has a better chance to work through corporate filters.

                  1. 1

                    If you have a github account for work, then you likely can push to that through ssh already.

                  2. 1

                    When I want to do that I usually just SSH from my work laptop to my personal one.

                2. 1

                  That’s nice. I usually work with a combination of direnv and setting GIT_SSH properly.

                  1. 1

                    I use this trick with my local GoCD hosting, since it doesn’t allow you to configure which ssh key you want to use for a version control checkout. Though it looks like I might swap to wbolster’s trick.

                    1. 1

                      nice post =)

                      1. 1

                        Related: remember that your commits are tied to an email address. If you want to associate them with your account long-term (eg, your open source work), be sure you use an email address you control long-term (eg, not a work address).

                        1. 1

                          I used this trick so you don’t have to change remote path or anything. Create a file kgit in ~/bin

                          #!/usr/bin/env bash
                          ssh -i $HOME/.ssh/[other-personal-ssh-key] $1 $2
                          

                          Whenever I need to switch to personal account:

                          export GIT_SSH=$HOME/bin/kgit
                          

                          This tell git when cloning/pushing via SSH it will use the right personal ssh key.

                          You can also create a shell function and put it into your rc file:

                          kgit () {
                          	export GIT_SSH=$HOME/bin/kgit
                          }
                          

                          Now simply run kgit in your shell.