Directories and config files for multiple GitHubs

31Mar20

Background

I use Enterprise GitHub at work, and public GitHub for my own projects and contributing to open source. As the different systems use different identities some care is needed to ensure that the right identities are attached to commits.

Directory structure

I use a three level structure under a ‘git’ directory in my home directory:

  • ~/git
    • which github
      • user/org
        • repo

 

  • ~/git – is just so that all my git stuff is in one place and not sprawling across my home directory
    • which github – speaks for itself
      • user/org – will depend on projects where I’m the creator, or I need to fork then pull versus stuff where I can contribute directly into various orgs without having to fork and pull
        • repo – again speaks for itself

So in practice I end up with something like this:

  • ~/git
    • github.com
      • cpswan
        • FaaSonK8s
        • dockerfiles
      • fitecluborg
        • website
    • github.mycorp.com
      • chrisswan
        • VirtualRoundTable
        • HowDeliveryWorks
        • Anthos
      • OCTO
        • CTOs

Config files

For commits to be properly recognised as coming from a given user GitHub needs to be able to match against the email address encoded into a commit. This isn’t a problem if you’re just using one GitHub – simply set a global email address (to the privacy preserving noreply address) and you’re good to go.

If you’re using multiple GitHubs then all is well if you’re happy using the same email across them, but there are lots of reasons why this often won’t be the case, and so it becomes necessary to use Git config conditional includes (as helpfully explained by Kevin Kuszyk in ‘Using Git with Multiple Email Addresses‘).

The top level ~/.gitconfig looks like this:

[user]
  name = Your Name
[includeIf "gitdir:github.com/"]
  path = .gitconfig-github
[includeIf "gitdir:github.mycorp.com/"]
  path = .gitconfig-mycorp

Then there’s a ~/.gitconfig-github:

[user]
  email = [email protected]

and a ~/.gitconfig-mycorp:

[user]
  email = [email protected]

With those files in place Git will use the right email address depending upon where you are in the directory structure, which means that the respective GitHub should be able to then match up the commits pushed to it to the appropriate user that matches those email addresses.



No Responses Yet to “Directories and config files for multiple GitHubs”

  1. Leave a Comment

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.