points by kondro 4 years ago

A new host with what hostname? Because git uses github.com for all repos.

3np 4 years ago

So one thing you can do is put the following in your ~/.gitconfig:

  [includeIf "gitdir:/home/john/corp/**"]
      path = .gitconfig-corp

And then ~/.gitconfig-corp:

  [user]
    email = john@example.com
    name = John Doe


  [url "git@github.com-corp"]
    insteadOf = git@github.com

Now all repos under the path /home/john/corp/ will use that config. Then you can put a new host in your SSH config:

  Host github.com-corp
    HostName github.com
    User git
    IdentityFile /home/john/.ssh/corp_github

This way you can have different e-mail address and name in your commit messages as well.

Due to the nature of git you can't scope it via GH URL (you can have many remotes in the same local repo). Though you can still manually rewrite when you add a remote with just the SSH config change, e.g.

  git clone git@github.com-corp:corp/foobar.git
  • renerthr 4 years ago

    Thank you. The `host` field always has to have the format `word.word`? Or it can be just `word` without any dot in the middle?

    • 3np 4 years ago

      You can call it anything you want save of whitespace (albeit I personally haven't tried emoji or other non-ASCII shenanigans :))

      • renerthr 4 years ago

        Thank you. Why is it that all examples in this thread use `.com` somewhere in the middle of the string used for the `host` field?

        • 3np 4 years ago

          Personally I just found it natural to tack on a suffix. But you do you :)

          • renerthr 4 years ago

            Why is it natural? Do hosts usually have a suffix? Why?

        • 3np 4 years ago

          I guess if you have a more complex matrix of personas and remote servers (not only GH), structure and predictability becomes more important than terseness.

          But again, it's just personal preference.

fnord77 4 years ago

put this in your ~/.ssh/config

    Host personal.github.com
          HostName github.com
          User git
          IdentityFile ~/.ssh/id_rsa_personal

then clone from git@personal.github.com/x.git for personal stuff

  • powersurge360 4 years ago

    The host also doesn’t have to be a “real” host. You could do something like github-personal for example.

    Thanks for the assist fnord77!

    • renerthr 4 years ago

      Newbie here. Where should I type `github-personal` ?

      • powersurge360 4 years ago

        In the example, fnord77 posted, you can replace all the places that they used personal.github.com. It will be usable anywhere you would use ssh, including when going through a few tools that ultimately resolve to an ssh invocation. I won't copy the host block in .ssh/config but you could, for example:

        `sshuttle -r my-cloud-server 0/0` for a poor man's vpn

        `ssh some-enterprise-server` for when the server has a user unfriendly domain name that you don't want to bother remembering

        `git clone git@github-personal:myusername/somerepo.git` when you want to clone using your personal key from GitHub.

        There is the caveat, though, that you may have some nested dependencies that will use the plain ole every day host name in which case things will break for you. It rarely comes up, though, in my experience.

  • kondro 4 years ago

    Wow! I don’t know why I hadn’t thought of that, even though I use aliases all the time for SSH. Seems my brain got stuck on the domain part. ^_^

  • AzN1337c0d3r 4 years ago

    How do you deal with submodules?

    • powersurge360 4 years ago

      This is a limitation that I haven't really found a way through. I haven't tried too hard though because I've found that I can often just let the complicating set up have the 'plain' github.com host and use the custom host for the other set up. If I found that both professionally and personally I needed submodules, though, I'd probably be in trouble.

      Also, if the submodules are public, the plain host will work fine because it doesn't matter what key you authenticate with.