Per-directory git config
Pantos May 04, 2024 #git #sshMultiple SSH keys
To generate a new key for each account or service:
If the keys are used for accounts in different services like github or bitbucket, we can have each service use a different ssh config.
Add to ~/.ssh/config:
Then each service will have a certain SSH key assigned, system-wide. So far so good, but this won't work for different accounts on the same service.
This can be tackled in a different way though, but let's first have alook at...
Multiple git identities
Let's assume we have different git identities (i.e. name/email) for each of the services, and we want git to automatically choose the right one.
-
Create two parent directories that each contains our repos for the according service, e.g.:
-
Add to
~/.gitconfig: -
Create
~/github/.gitconfig: -
Create
~/bitbucket/.gitconfig:
With this, git will pick the an identity, depending on the parent directory that the git repo is
in. This will not be system-wide, so if we want to add new identities, we will have to create
additional parent dirs and add new includeIf entries.
Different SSH keys depending on parent directory
Instead of using the SSH config file, we can use the different git config files in the respective parent directories to pick the right SSH key.
-
Add to
~/github/.gitconfig: -
Add to
~/bitbucket/.gitconfig:
This can even be used for different git accounts on the same service. Depending on the parent directory, a certain author, email and ssh key will be used.