Setup multiple git and ssh identities
Sometimes when You have multiple ssh identities You need to manage which git repository uses which key. This post will try to help You with that problem.
Setup multiple ssh identities
- Generate your SSH keys as per your git provider documentation.
- Add each public SSH keys to your git providers acounts.
-
In your
~/.ssh/config
, set each ssh key for each repository as in this exemple:Host github.com HostName github.com User git IdentityFile ~/.ssh/github_private_key IdentitiesOnly=yes Host gitlab.com Hostname gitlab.com User git IdentityFile ~/.ssh/gitlab_private_key IdentitiesOnly=yes
Setup dynamic git user email & name depending on folder
/!\ Be very carrefull in your setup : any misconfiguration make all the git config to fail silently ! Go trought this guide step by step and it should be fine :wink:
The idea here is to use a different git user name & email depending on the folder you are in.
-
In your
~/.gitconfig
, remove the[user]
block and add the following (adapt this exemple to your needs) :[includeIf "gitdir:~/code/personal/"] path = .gitconfig-personal [includeIf "gitdir:~/code/professional/"] path = .gitconfig-professional
-
In your
~/.gitconfig-personal
, add your personnal user informations:[user] email = [email protected] # note we use the noreply github mail name = personal_username
-
In your
~/.gitconfig-professional
, add your professional user informations:[user] email = [email protected] name = professional_username
That's it for this post, thanks for reading!