DEV Community

Discussion on: Using git with multiple profiles and GPG+SSH keys

Collapse
 
asaaki profile image
Christoph Grabo • Edited

Hi there,

For Q1: No, you don't have to do that at all, that's why we have the following piece in the git config:

[url "git@github.com-work"]
  insteadOf = git@github.com
Enter fullscreen mode Exit fullscreen mode

and the matching ssh config:

Host github.com-work
  Hostname github.com
  User <YOUR GITHUB USERNAME>
  IdentityFile ~/.ssh/<YOUR WORK SSH KEY>
Enter fullscreen mode Exit fullscreen mode

So when you do git stuff in your work repositories, git will first rewrite the URLs to git@github.com-work and the underlying SSH will then use this host to look up the related configuration part and make calls to the real github.com again. The git@github.com-work address becomes just a label between git and ssh, but you as a user shouldn't need to do anything manually.

For Q2: The switching is done through convention, meaning: it depends on where the repository lives on your disk.

[includeIf "gitdir:~/Development/Personal/"]
  path = ~/.gitconfig.personal

[includeIf "gitdir:~/Development/Work/"]
  path = ~/.gitconfig.work
Enter fullscreen mode Exit fullscreen mode

The convention is, personal projects shall go into the ~/Development/Personal/ folder, and work related repos in ~/Development/Work/.

includeIf is as the name suggest a conditional loading, the condition is "where on the computer is the action happening".

Note: to also support repos outside of those two locations, the personal profile is the default for everything. You can decide on your work machine to use instead the work profile instead; for that adjust the config location of the following piece:

# this is the default for any location on your computer
[include]
  path = ~/.gitconfig.personal # change that if needed
Enter fullscreen mode Exit fullscreen mode

To Q3: As already touched by the answer to Q2, the switching happens solely based on the location on your computer. That is literally the only "magic" in this whole approach. Place the repos into the right locations and the matching configuration will be used.

Does that clarify it for you?

Collapse
 
samuelpierra profile image
samuel

Yes, I can't thank you enough.