How to use multiple SSH keys when working with different github repositories
Topic posted the 2023-03-23 in the category
Développement
Topic updated: 2023-03-23
A very small tutorial how to use multiple SSH keys when working on different Github repositories.
Sometimes you might want to have multiple SSH keys on the same computer while working with multiple Github repositories. There might be many reasons, for instance security policies, and having a SSH key for your personal work and another one for your professional one. Let's focus on this example, and let's go.
The context
- You want to work with two different SSH keys on many different Github repositories.
- Your professionnal SSH key has the default naming (ie id_rsa and id_rsa.pub).
- Your personal SSH key has the following naming: id_rsa_john.
- Both keys are stored in the default .ssh folder of your OS.
- Your global (OS-wide) git configuration has your name and professional email, so by default, if you don't override it at the repository level, it will use this email to sign your commits.
Setting the SSH configuration
Let's edit your ~/.ssh/config file and add the following:
Host gh-john
User git
Hostname github.com
IdentityFile ~/.ssh/id_rsa_john
Here we defined a specific host to handle your personal key.
Cloning a repository
Let's assume you want to clone my GMG repository. If you want to use your professional key, just use the standard procedure to clone the repository locally:
git clone git@github.com:ecourtial/gmg.git .
But if you want to use your personal one, do this :
git clone gh-john:ecourtial/gmg.git .
Changing the email to sign your commits
If working on a personal project, edit the .git/config file at the project level, and add the following:
[user]
name = John Doe
email = myPersonalEmailAddress@bar.com
That's it!
Cet article vous a plu? Découvrez d'autres articles :